Python Docx Demo
By Dr. Louay Chebib
https://github.com/Netuser-Py/PythonDocx
Python Docx Demo
- Create or modify MS Word 2007 files from inside Python.
- MS Word does not need to be installed on the local processor.
-
Standards: ‘WordML’, ‘Office Open XML’ and ‘Open XML’ by Microsoft.
https://python-docx.readthedocs.io/en/latest/index.html
Python Docx Demo
Review of MS Word
- MS word tools and structures
- Styles
- Paragraphs
- Tables
- Templates
Python Docx Demo
Easy to Install
- pip install docx (2.7)
- pip install python_docx (3.x)
Python Docx Demo
Sample Part 1
import docx
Create a blank word file in memory
Add headings
Add Table
Add Table Headings
Add Table Details
Add a page break
Open a Template/Form .docx file that you created in word
Merge this into doc_work
Save the file from memory
Python Docx Demo
Sample Part 2
Use a template word file to create a non standard size page
Open a Template/Form .docx file that you created in word 4 x 6 inch form
list the main paragraph text (not the table text)
Load the paragraphs into a list
overwrite the contents of paragraphs
replace as needed
now look through the tables 0 & 1 and change paragraphs in each table
save doc
Python Docx Demo
Sample Part 3
new doc_out document in memory for merge demo
merge work to doc_out
merge doc into doc_out. Note that somehow it follows the layout of the 4 x 6 template
Note: last document merged controls
we just merged doc_work then doc.
what happens if we change the order? try It!
Python Docx Demo
Bonus pprint Sample
Print a docx file in the background using MS Word from inside Python
see: pprint_sample.py
Python Docx Demo
By Dr. L. Chebib
Python Docx Demo
Python Docx Demo
- 1,614