web123456

【Python-Office Automation】Batch modify all WORD document formats in folders

# Import necessary libraries and functions from docx import Document from docx.oxml import OxmlElement from docx.shared import Pt # Used to set the font size (point value) from docx.oxml.ns import qn # Used to specify a namespace in OpenXML to apply Chinese fonts # Define functions to set the font format of different parts def F_title(run): # Title text run.font.size = Pt(22) # Set text size to 22 pounds run.bold = False # Bold run.font.name = "Fang Zheng Xiaobiao Song Simplified Chinese" # Set the font to "Fangzheng Xiaobiao Song Simplified Chinese" r = run._element.rPr.rFonts # Get the font settings object r.set(qn("w:eastAsia"), "Fang Zheng Xiaobiao Song Simplified Chinese") # Set the Chinese font separately to "Fangzheng Xiaobiao Song_GBK" def F_name_dept(run): #name, department, date run.font.size = Pt(17) #Text size pound value run.bold = False #Bold run.font.name = "Kai Style" #Font r = run._element.rPr.rFonts r.set(qn("w:eastAsia"),"Kai Style") #Font def F_main(run): #Future of text run.font.size = Pt(17) #Text size pound value run.bold = False #Bold run.font.name = "Imitation Song" #Font r = run._element.rPr.rFonts r.set(qn("w:eastAsia"),"Imitation Song") #Font def F_title1(run): #Title One Format run.font.size = Pt(17) #Text size pound value run.bold = False #Bold run.font.name = "Bold" #Font r = run._element.rPr.rFonts r.set(qn("w:eastAsia"),"Bold") #Font def F_title2(run): #Title Two Format run.font.size = Pt(17) #Text size pound value run.bold = True #Bold run.font.name = "Kai Style" #Font r = run._element.rPr.rFonts r.set(qn("w:eastAsia"),"Kai Style") #Font # Import docx and os libraries import docx, os # Set the folder path where the pending file is located path = r'C:\Users\Administrator\Desktop\Audit Required Data1' # Note: This should be a specific folder path, such as 'C:\\path\\to\\folder' files = [path + "\\" + i for i in os.listdir(path)] # Get the file name in the folder and splice it into a complete path # Process files one by one for file in files: doc = docx.Document(file) # Load Word Documents # Set the font format of the total title for run in doc.paragraphs[0].runs: F_title(run) # Set the font format of department, name and date for para in doc.paragraphs[1:3]: for run in para.runs: F_name_dept(run) # Define the unique feature string of title one and title two title1 = ["one,", "two,", "three,", "Four,", "five,", "six,", "seven,", "eight,", "Nine,", "ten,"] title2 = ["(one)", "(two)", "(three)", "(Four)", "(five)", "(six)", "(seven)", "(eight)", "(Nine)", "(ten)"] # traverse the rest of the document and set the font format according to the content for para in doc.paragraphs[3:]: if any(i in para.text for i in title1): # If the paragraph contains the feature string of title one for run in para.runs: F_title1(run) elif any(j in para.text for j in title2): # If the paragraph contains the feature string of title two for run in para.runs: F_title2(run) else: for run in para.runs: # Otherwise, apply the font format of the body F_main(run) # Save the processed document to the specified folder, and the file name remains unchanged doc.save('C:\\Users\\Administrator\\Desktop\\Audit Required Data 1\\{}'.format(file.split("\\")[1]+'.docx')) # Note: Here it is assumed that 'processed file' is an existing folder