How do I apply both bold and italics in python-docx?
<p>I'm working on making a dictionary. I'm using  python-docx to put it into MS Word. I can easily make it bold, or italics, but can't seem to figure out how to do both. Here's the basics:</p>

<pre><code>import docx

word = 'Dictionary'

doc = docx.Document()
p = doc.add_paragraph()
p.add_run(word).bold = True

doc.save('test.docx')
</code></pre>

<p>I have tried p.add_run(word).bold.italic = True, but receive a 'NoneType' error, which I understand.</p>

<p>I have also tried p.bold = True and p.italic = True before and after the add_run, but lose formatting all together.</p>

<p>Word's find/replace is a simple solution, but I'd prefer to do it in the code if I can.</p>

<p>The <code>add_run</code> method will return a new instance of <code>Run</code> each time it is called. You need create a single instance and then apply <code>italic</code> and <code>bold</code> </p>

<pre><code>import docx

word = 'Dictionary'

doc = docx.Document()
p = doc.add_paragraph()

runner = p.add_run(word)
runner.bold = True
runner.italic = True

doc.save('test.docx')
</code></pre>

python docx python-docx