Writing a basic Word document
Microsoft (MS) Office is one of the most common pieces of software, and MS Word in particular is almost the de facto standard for editable documents. Generating docx
documents is possible with automated scripts, which may help distribute reports in a format that's easily shared in many businesses.
In this recipe, we will learn how to generate a full Word document programmatically.
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
python-docx
anddatetime
:>>> import docx >>> from datetime import datetime
- Define the
context
with the data to be stored in the report:>>> context = { ... 'date': datetime.now(), ... 'movies': ['Casablanca', 'The Sound of Music'...