In daily work, handling a large number of Word documents is a common task, especially when it requires batch modification of the style of the document. Manual operations are time-consuming and error-prone. Fortunately, Python provides a wealth of libraries that can help automate this process. This article will introduce in detail how to use Python to batch style Word documents and include specific sample code to help complete this task more efficiently.
Environmental preparation
Before you start writing your code, you need to make sure that Python is installed (using Python 3 in this article) and that the library python-docx is installed, which is required to handle Word documents.
The python-docx library can be installed using the following command:
pip install python-docx
Basic Operation
Open and read Word documents
Here is a simple example showing how to open and read the contents of a Word document using python-docx:
from docx import Document # Open Word documentdoc = Document('') # Read document contentfor paragraph in : print()
In this example, use the Document class to open a Word document named, and iterate through each paragraph in the document, printing its text content.
Modify paragraph style
Next, you will show how to modify the style of the paragraph. Suppose you set the font of all paragraphs to Arial and the font size to 12.
from import Pt from import qn from import OxmlElement def set_paragraph_style(paragraph): run = [0] = 'Arial' = Pt(12) # Set Chinese fonts r = run._element rPr = r.get_or_add_rPr() eastAsia = OxmlElement('w:eastAsia') (qn('w:val'), 'Song style') (eastAsia) # Open Word documentdoc = Document('') # Modify the style of all paragraphsfor paragraph in : set_paragraph_style(paragraph) # Save the modified document('modified_example.docx')
In this example, a function set_paragraph_style is defined to set the font and font size of a paragraph, and iterate through each paragraph in the document, and call the function to modify the style. Finally, save the modified document as modified_example.docx.
Batch processing of Word documents
In order to batch process multiple Word documents, the above code can be encapsulated into a function, and iterate through all Word documents in the specified directory to make style modifications.
Functions that batch modify document styles
import os from docx import Document from import Pt from import qn from import OxmlElement def set_paragraph_style(paragraph): run = [0] = 'Arial' = Pt(12) # Set Chinese fonts r = run._element rPr = r.get_or_add_rPr() eastAsia = OxmlElement('w:eastAsia') (qn('w:val'), 'Song style') (eastAsia) def process_word_file(file_path): doc = Document(file_path) for paragraph in : set_paragraph_style(paragraph) new_file_path = ('modified_files', (file_path)) (new_file_path) def batch_process_word_files(directory): if not ('modified_files'): ('modified_files') for filename in (directory): if ('.docx'): file_path = (directory, filename) process_word_file(file_path) print(f"Processed files: {file_path}") if __name__ == "__main__": directory = 'word_files' batch_process_word_files(directory)
In this example, the following functions are defined:
set_paragraph_style(paragraph): Set the font and font size of the paragraph.
process_word_file(file_path): Process a single Word document, modify its style and save it to a new directory.
batch_process_word_files(directory): batch process all Word documents in the specified directory and save the modified documents to the modified_files directory.
Run batch processing scripts
Save the above code as batch_modify_word_styles.py and run it in the command line:
python batch_modify_word_styles.py
Make sure that the Word document you need to process is placed in the word_files directory before the script runs. After the script is run, the modified document will be saved to the modified_files directory.
Example: Modify different styles
In addition to modifying the paragraph style, you can also modify the style of the title, table, and picture.
Modify the title style
def set_heading_style(paragraph): if ('Heading'): run = [0] = 'Arial' = Pt(14) = True def process_word_file_with_headings(file_path): doc = Document(file_path) for paragraph in : set_paragraph_style(paragraph) set_heading_style(paragraph) new_file_path = ('modified_files', (file_path)) (new_file_path)
Modify the table style
def set_table_style(table): for row in : for cell in : for paragraph in : set_paragraph_style(paragraph) def process_word_file_with_tables(file_path): doc = Document(file_path) for paragraph in : set_paragraph_style(paragraph) for table in : set_table_style(table) new_file_path = ('modified_files', (file_path)) (new_file_path)
Modify the picture style
Modifying the picture style usually involves more complex operations, and the specific implementation depends on the needs.
Here is a simple example to resize the image:
from import Inches def set_picture_style(document): for paragraph in : for run in : for inline_shape in run.inline_shapes: inline_shape.width = Inches(2) inline_shape.height = Inches(2) def process_word_file_with_pictures(file_path): doc = Document(file_path) for paragraph in : set_paragraph_style(paragraph) set_picture_style(doc) new_file_path = ('modified_files', (file_path)) (new_file_path)
Summarize
This article details how to use Python to batch style Word documents. By using the python-docx library, we can open, read and modify paragraphs, titles, tables, and image styles in Word documents. The article first shows the basic operations, including opening a document and modifying paragraph styles, and then further introduces how to batch process multiple Word documents. Finally, sample code for modifying the style of titles, tables, and pictures is also provided. Mastering these skills can significantly improve office efficiency and realize automated processing of documents.
This is the article about Python's automation and batch adjustment of Word styles. For more related content on Python's batch adjustment of Word styles, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!