Styling a Word document
A Word document can contain text with almost no format, but we can also add styling to help us understand the displayed content. Word has a set of predefined styles that can be used to variate the document and highlight the important parts of it.
Getting ready
We'll use the python-docx
module to process Word documents:
$ echo "python-docx==0.8.10" >> requirements.txt
$ pip install -r requirements.txt
How to do it...
- Import the
python-docx
module:>>> import docx
- Create a new document:
>>> document = docx.Document()
- Add a paragraph that highlights some words in different ways (Italics, bold, and underline):
>>> p = document.add_paragraph('This shows different kinds of emphasis: ') >>> p.add_run('bold').bold = True >>> p.add_run(', ') <docx.text.run.Run object at ...> >...