我需要合并两个包含图像的docx文件。 下一个代码合并文件(文本,表格),但不能合并图像。
请问有什么解决办法? :)
import os
from docx import Document
files = ['sub_doc1.docx', 'sub_doc2.docx']
def merge_docs(files):
res_doc = Document()
for file in files:
sub_doc = Document(file)
for element in sub_doc.element.body:
res_doc.element.body.append(element)
res_doc.save('res_doc.docx')
os.startfile('res_doc.docx')
merge_docs(files)
要合并的文件和结果文件在这里:
答案 0 :(得分:0)
将文档转换为pdf,然后将pdf合并到一个文件中。
import os
import glob
import comtypes.client
from PyPDF2 import PdfFileMerger
def docxs_to_pdf():
"""Converts all word files in pdfs and append them to pdfslist"""
word = comtypes.client.CreateObject('Word.Application')
pdfslist = PdfFileMerger()
x = 0
for f in glob.glob("*.docx"):
input_file = os.path.abspath(f)
output_file = os.path.abspath("demo" + str(x) + ".pdf")
# loads each word document
doc = word.Documents.Open(input_file)
doc.SaveAs(output_file, FileFormat=16+1)
doc.Close() # Closes the document, not the application
pdfslist.append(open(output_file, 'rb'))
x += 1
word.Quit()
return pdfslist
def joinpdf(pdfs):
"""Unite all pdfs"""
with open("result.pdf", "wb") as result_pdf:
pdfs.write(result_pdf)
def main():
"""docxs to pdfs: Open Word, create pdfs, close word, unite pdfs"""
pdfs = docxs_to_pdf()
joinpdf(pdfs)
main()