Writing data into Word documents (adding headings, images, tables)
Reading files is a breeze with the
python-docx module. Now, let's shift our focus to writing Word documents. We'll perform multiple operations with documents in this section.
For this recipe, we will use the same fantastic Python module,
python-docx. We don't need to spend much time on the setup. Let's get started!
How to do it...
- We start with a very basic operation of creating a
.docxfile and then to add a heading to it. The following code performs this operation:
from docx import Document document = Document() document.add_heading('Test Document from Docx', 0) document.save('testDoc.docx')
This is how the document looks:
- If you look at the screenshot, you will see ...