使用python-docx合并Word文档
我有几个Word文件,每个文件里面都有特定的内容。我想要一个代码片段,能帮我把这些Word文件合并成一个文件,使用Python的docx
库。
比如在pywin32库中,我做了以下操作:
rng = self.doc.Range(0, 0)
for d in data:
time.sleep(0.05)
docstart = d.wordDoc.Content.Start
self.word.Visible = True
docend = d.wordDoc.Content.End - 1
location = d.wordDoc.Range(docstart, docend).Copy()
rng.Paste()
rng.Collapse(0)
rng.InsertBreak(win32.constants.wdPageBreak)
但是我需要用Python的docx
库来完成这个,而不是用win32.client
。
7 个回答
3
如果你只是想把一些简单的文本文件合并在一起,可以使用上面提到的python-docx。
如果你需要合并的文件里有超链接、图片、列表、项目符号等等,那你可以用lxml来把这些文件的内容和所有相关的文件结合起来,比如:
- word/styles.xml
- word/numbering.xml
- word/media
- [Content_Types].xml
5
首先,创建一个空的文档,命名为empty.docx,然后把你要合并的两个文档添加到这个空文档里。
在处理每个文件的时候,如果需要的话,就插入一个分页符。
完成后,保存这个新文件,它里面包含了你合并的两个文件。
from docx import Document
files = ['file1.docx', 'file2.docx']
def combine_word_documents(files):
combined_document = Document('empty.docx')
count, number_of_files = 0, len(files)
for file in files:
sub_doc = Document(file)
# Don't add a page break if you've
# reached the last file.
if count < number_of_files - 1:
sub_doc.add_page_break()
for element in sub_doc._document_part.body._element:
combined_document._document_part.body._element.append(element)
count += 1
combined_document.save('combined_word_documents.docx')
combine_word_documents(files)
22
我已经把上面的例子调整了一下,让它能在最新版本的 python-docx(写这段话时是0.8.6版本)上运行。请注意,这个例子只是复制了元素(合并元素的样式要复杂得多):
from docx import Document
files = ['file1.docx', 'file2.docx']
def combine_word_documents(files):
merged_document = Document()
for index, file in enumerate(files):
sub_doc = Document(file)
# Don't add a page break if you've reached the last file.
if index < len(files)-1:
sub_doc.add_page_break()
for element in sub_doc.element.body:
merged_document.element.body.append(element)
merged_document.save('merged.docx')
combine_word_documents(files)
36
合并两个文档并保留所有样式的另一种方法是使用Python库docxcompose(https://pypi.org/project/docxcompose/)。这样我们就不需要明确地定义样式,也不用一段一段地读取文档并把它们添加到主文档中。下面的代码展示了如何使用Python的docxcompose。
#Importing the required packages
from docxcompose.composer import Composer
from docx import Document as Document_compose
#filename_master is name of the file you want to merge the docx file into
master = Document_compose(filename_master)
composer = Composer(master)
#filename_second_docx is the name of the second docx file
doc2 = Document_compose(filename_second_docx)
#append the doc2 into the master using composer.append function
composer.append(doc2)
#Save the combined docx with a name
composer.save("combined.docx")
如果你想把多个文档合并成一个docx文件,可以使用下面的函数。
#Filename_master is the name of the file you want to merge all the document into
#files_list is a list containing all the filename of the docx file to be merged
def combine_all_docx(filename_master,files_list):
number_of_sections=len(files_list)
master = Document_compose(filename_master)
composer = Composer(master)
for i in range(0, number_of_sections):
doc_temp = Document_compose(files_list[i])
composer.append(doc_temp)
composer.save("combined_file.docx")
#For Example
#filename_master="file1.docx"
#files_list=["file2.docx","file3.docx","file4.docx",file5.docx"]
#Calling the function
#combine_all_docx(filename_master,files_list)
#This function will combine all the document in the array files_list into the file1.docx and save the merged document into combined_file.docx
5
如果你的需求比较简单,可以试试下面这个方法:
source_document = Document('source.docx')
target_document = Document()
for paragraph in source_document.paragraphs:
text = paragraph.text
target_document.add_paragraph(text)
当然,还有其他的做法,但这个应该能让你入门。
其实,把一个Word文件里的内容复制到另一个文件里是个挺复杂的事情。比如说,源文件里的样式可能和目标文件里的样式有冲突,所以需要处理这些问题。因此,这个功能在接下来的一年里可能不会被添加进来。