preamble
This module can be used to perform bulk editing of word documents through python code. docx It provides a feature-rich set of functions and methods for creating, modifying and reading Word documents. Here aredocx
An introduction to some of the commonly used functions and methods in the module:
Installation:
pip install docx
I. Prepare a word document
The content of the document is roughly as follows
Second, read the contents of the document
1. Read the content of the paragraph
Iterate through to get the text of each paragraph in the document.。
from docx import Document path = '' #word document path doc = Document(path) # Create a document object # Iterate over all paragraphs in a document for par in : text = print(text)
The output is as follows:
2, read the contents of the form
from docx import Document path = '' #word document path doc = Document(path) # Create a document object # Iterate over tables in a document for table in : for row in : for cell in : text = print(text)
The output is as follows:
III. Modifications
Modify the content need to pay attention to the need to work with (file_path): save the document to the specified file path file_path.
1、Modify the content of the document
For example, replace "all 123's" in the document with "study hard."
from docx import Document path = '' #word document path doc = Document(path) # Create a document object # Iterate over all paragraphs in a document for par in : text = if '123' in : = ('123','Study hard!') #Where there is a save function, you need to close the original document before you can edit it (path) # Note that if the path to the original document is used here, the content of the original document will be modified directly. new_path = 'new_test.docx' # Create a new path (new_path) #Here, the content is saved in a new document.
Output results:
Because I'm saving two paths here:
1, you can see here a newly generated word document
2, view the contents of the two documents
Original Document:
Newly generated document:
Summary:
1. Thoughts:
Locate the modifications first - > then assign - > save (anything not modified is saved as is)
2. You need to close the original document when running the modified function;
3, if you do not generate a new document then bear in mind that the modifications are not reversible!!!!
2. Modify the content of the form
Replace "break" with "prep" in the table.
A few more tables can be copied here
Follow the thoughts above:
1, first locate, find the coordinates of the modified target
from docx import Document path = '' #word document path doc = Document(path) # Create a document object tables = # Get the coordinates of all the content in the table. for i, row in enumerate(tables[0].rows): for j, cell in enumerate(): print( f'[+]{} [+]The corresponding coordinates are ({i}, {j})\n')
Output results:
Note here (finding coordinates here is not the same as our usual habit of looking at a table, they are not logically related, but simply coordinates to correspond)
2, remember this coordinates we can later modify all the tables:
Note that the coordinates in cell(2,1) in the code below are the coordinates we found above, through tables[tab] is traversed one by one out of each table, and then the corresponding coordinates document replaced with 'prep'.
from docx import Document path = '' #word document path doc = Document(path) # Create a document object tables = # Here all the tables in the document have been taken out and stored as a list # Get the coordinates of all the content in the table. for tab in range(len(tables)): tables[tab].cell(2,1).text='Pre-reading' # # Where there is a save feature, you need to close the original document before you can edit it (path) #Note that if you use the path to the original document,Then the content of the original document will be modified directly.,
Output results:
to this article on the use of Python docx module editing Word documents to this article, more related Python docx editing Word content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!