formatting a run within a loop and inserting in a table Python docx
Question: I am trying to format text using python docx. The text is a string pulled out
by a loop. How can I name the object a run and then apply a font to the run?

This is my code:

    
    
    xcount = 0
    while xcount < xnum:
        xdata = datastring[xcount]
        obj1 = xdata[0]
        run = obj1
        font = run.font
        from docx.shared import Pt
        font.name = 'Times New Roman'
        row1.cells[0].add_paragraph(obj1)
        xcount += 1
    

I get the error:

    
    
    AttributeError: 'str' object has no attribute 'font'
    


Answer: `xdata[0]` is a string (doesn't have a `.font` attribute). You'll need to
create a `Document()`, add a paragraph, and add a run to that. E.g.:

    
    
    from docx import Document
    from docx.shared import Inches
    
    document = Document()
    
    document.add_heading('Document Title', 0)
    
    p = document.add_paragraph('A plain paragraph having some ')
    p.add_run('bold').bold = True
    p.add_run(' and some ')
    p.add_run('italic.').italic = True
    

(copied directly from the docs: <http://python-
docx.readthedocs.io/en/latest/>)

