Python Office Automation – Processing word documents
At the end of the project bidding, a lot of content needs to be sorted out. Many contents of the bidding document are actually repeated before the bidding document, so we can combine the corresponding parts of the content and then deal with the format. If you use the normal operation each operation need to open the subdirectory — > find word document — > select all copy — > paste to a new document — > adjust the format — > check and check, error during the use of more time, this is a waste of time!
Using Python’s intergroup object model (COM) to design a program to merge multiple Word files at the same time, the code is as follows:
Import win32com. Client as win32 import OS word = win32. Gencache.. EnsureDispatch (' word. Application ') # start word Application object word.Visible = False path = r'D:\doc' files = [] for filename in os.listdir(path): Join (path,filename) files.append(filename) output = word.documents.Add() for file in files: Output. The Application. The Selection. InsertFile (file) # joining together the Content of the document # for the combined document doc. = the output Range (the output. The Content. The Start, Output.content.end) output.saveas (r'd ://doc//result.docx')Copy the code
It took about two or three minutes for a 110-page, 20,000-word report to be automatically completed,
Some other uses of the Win32com library
1. Add multilevel headings
import win32com.client as win32 from win32com.client import constants doc_app = Win32. Gencache. EnsureDispatch (' Word. Application ') # open Word Application doc_app. Visible Visible doc = = 1 # set up the Application Parag = doc.documents.Add()# Add new Paragraphs parag_range = parag Parag_range. Style = doc.Styles("Heading 1")#Heading 1 is the name of the Style border = parag.Borders(constants.wdBorderBottom) border.LineStyle = constants.wdLineStyleSingle border.LineWidth = The wdLineWidth225pt # 2 parag_range. Add the title Text + = '\ n' parag = doc. Paragraphs. The Add () parag_range = parag. Range Parag_range. Text =' Heading 2' parag_range.Style = doc.Styles("Heading 2")# # Add Text parag_range.Text +='\n' parag = Add() parag_range = parag.range parag_range.text =' Text 'parag_range.style = doc.styles ("Normal")# TextCopy the code
Using add does not add a new paragraph, but points to the current paragraph.
2. Insert the directory
import win32com.client as win32 from win32com.client import constants doc_app = Win32. Gencache. EnsureDispatch (' Word. Application) doc_app. Visible = 1 doc = doc_app. Documents. The Add () added title 1 # # # parag = text Add() parag_range = parag.range parag_range.text =' Heading 1' ### set parag_range.style = doc.styles ("Heading # 1 ") Heading 1 style is the name of the border = parag. Borders (the wdBorderBottom) border. Graphics.linestyle = the wdLineStyleSingle Border. Our LineWidth = constants. WdLineWidth225pt # 2 parag_range. Add the title InsertParagraphAfter (#) after the current position, Parag_range = parag.range parag_range.Text =' Heading 2' parag_range.Style = doc.styles ("Heading 2")# secondary Heading ## Add Text Parag_range. InsertParagraphAfter () parag_range = parag. Range parag_range. Text = 'body' parag_range. Style = Doc.Styles("Normal")# add/subtract/print/print/print/print/print Doc. Paragraphs (1). Range. InsertParagraphBefore (#) in the first line before inserting a row, Parag_range = doc. Range (1).Range# point to the newly inserted row # insert directory ## insert the directory from the currently pointed position, set the directory style using the default style. TablesOfContents.Add(Range= Parag_range, UseHeadingStyles=True,LowerHeadingLevel=3)Copy the code