Use Python-docx to generate Word documents

Writing documents has always been a tedious task, especially when writing some API documents. Because the format of this kind of file is fixed, but it must be written and often needs to be modified, it always makes people feel upset. Therefore, before that, I wanted to use automation to generate word files. And recently studying AI, feeling that automatically generating files can be combined with, so first do a technical reserve.

Use Python to Generate Word DOC

To generate a Word document in Python, you can use the python-docx library. To install it, use pip.

python-docx can either open a brand new file or open an existing file as a template. It’s recommended to just try it out and then customize your own template.

Python-docx

The Python-docs support the addition of features such as headings, paragraphs, and pictures, which allows you to construct titles, content, images, tables, and figures. The parts of tables and figures can also be assigned labels for display in the table of contents.

In order to simplify the use of python-docx, I separated its functions into several add_heading, add_paragraph, and add_list_bullet in another file, so that it can look more concise when used.

Download project

Download the demo project with the following command.

Generate Word document

The generated results

Open demo2.docx and you can see the following content.

 

The Code

Main Program

The main program utilizes the function provided by gen_wordtool.py to construct files. In principle, there is nothing particularly special about it, but it is more concise.

L1-L5: Functions of the import docx module.

L6~9: Import the functions written in gen_wordtool.py.

L11: Modify using the demo.docx template.

L13-15: Add titles and content with bolded text.

Add list items.

L24-27: Add numerical list

L31-32: Add images and their captions.

Add tables, set their style to “Light Shading Accent 1” and add borders. The official document website mentions the available styles.

L56: Add table numbers to the table.

L57 to L59 should be inserted and saved.

gen_wordtoolpy

“gen_wordtool.py is a script that combines the API of python-docx to create a simpler syntax for usage.”

There are several complex functions involved.

  • add_caption(): Adds a caption to a chart or table so that it can be displayed in the chart or table of contents. It uses function variables such as STYLEREF, SEQ, and ARABIC, which are difficult to understand. The fastest way is still to observe the function variables from an already established file. In Word, press ALT + F9 to switch display function code or content.
  • set_table_border(): Adds borders to a table.

 

Conclusion

Word may not be considered a good format, but it is very widely used. The advantage is that it also follows certain standards, which allows us to make some modifications using Python. This lays the foundation for future automation generation.

Leave a Reply(Name請以user_開頭,否則會被判定會垃圾息)

請輸入答案 85 ÷ 85 =