Send Close Add comments: (status displays here)
Got it!  This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Python: OpenXML docx files
by RS  admin@robinsnyder.com : 1024 x 640


1. Python: OpenXML docx files
Python can be used to both create and read docx files in the OpenXML data format. The Python python-docx package will be used. One way to install the python-docx package is as follows.
D:\Python38\Scripts\pip3.exe install python-docx

Use the path to your installed version of Python.

2. Getting started
Besides this page, more information on getting started is at the python-docx web site at https://python-docx.readthedocs.io/en/latest/user/quickstart.html.

3. Document structure
A simplified document (tree) structure is as follows. Each of a document, section (heading), paragraph, run, etc., has certain properties and methods.

4. Class
It is often useful to create your own class for package usage and then abstract that class and other useful features of the package access to a module.

For now, explicit code is used as an example. Here is the Python code [#1]

Here is the output of the Python code.

Empty bar chart

5. To be added
To add bold, italic, etc., add runs to the paragraph and set those attributes for the run.

To change the styles for headers, etc., backup and then change the style file dotx file in the python-docx package installation. Open that style file and make the desired changes.

In my installation, that file is in the following location.
D:\Python38\Lib\site-packages\docx\templates\default.docx


6. Locked files
Note that Word (and other programs) will lock the file currently loaded into the application.

If you try to generate a docx file while Word has it locked, a run-time error results.

7. End of page

by RS  admin@robinsnyder.com : 1024 x 640