Generating structure in Word documents
To create proper professional reports, they need to be properly structured. An MS Word document doesn't have the concept of a page, as it works in paragraphs. But we can introduce breaks and sections to properly divide a document.
We'll see in this recipe how to create a structured Word document, introducing breaks to create sections.
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()
- Create a paragraph that has a line break:
>>> p = document.add_paragraph('This is the start of the paragraph') >>> run = p.add_run() >>> run.add_break(docx.text.run.WD_BREAK...