利用python-docx和docxcompose实现word合并,自动化办公

期末临近,老师需要合并学生提交的作业,作业是word文档,合并到一个,利于阅卷
想起前几天看过的一篇python自动化办公的帖子,想自己动手试一试

库需求:python-docx、docxcompose、os
输入工作目录,os模块会读取该目录下的文件列表,将其保存在files列表中
遍历该列表,将里面的word文档都插进去

通俗点讲,就是你输入你把要合并的文档都放在一个目录,目录里不要有其他东西
把目录名复制上来,目录里所有的文档将会被打包
你输入要输出文档的名字,他会在目录里输出合并后的文档

import os
from docx import Document
from docxcompose.composer import Composer
print("The software is designed by STL_CC")
print("For more details and useage,just get to my blog:")
print("https://blog.csdn.net/STL_CC/article/details/106387603")
path=input("Please tell me the working directory:")
name=input("Please tell me the name of the new document:")
files = []
for filename in os.listdir(path):
    filename = os.path.join(path,filename)
    files.append(filename)
new_document = Document()
composer=Composer(new_document)
for f in files:
	composer.append(Document(f))
composer.save(path+'\\'+name+'.docx')

学委没装python环境,装起来够麻烦的
所以将程序又用pyinstaller打了包
pyinstaller -F test.py

猜你喜欢

转载自blog.csdn.net/STL_CC/article/details/106387603