Word
1. 把所有word转化为PDF
# 转载来源:https://blog.csdn.net/ayouleyang/article/details/108283956
from win32com.client import Dispatch
import os
pdfRoot = "D:\Desktop\wordToPDF\pdf" #保存pdf结果的文件夹
wordRoot = "D:\Desktop\wordToPDF\word" #读取word的文件夹
def doc2pdf(filePath, file):
print("正在转换:",file)
word = Dispatch('Word.Application')
doc = word.Documents.Open(filePath)
outFile = pdfRoot +"\\"+ file.split('.')[0] + ".pdf" #生成pdf文件路径名称
doc.SaveAs(outFile, FileFormat=17)
doc.Close()
word.Quit()
if __name__ == "__main__":
filelist = os.listdir(wordRoot)
for file in filelist:
if (file.endswith(".doc") or file.endswith(".docx")) and ("~$" not in file):
filePath = wordRoot+"\\"+file
doc2pdf(filePath, file)
print ("所有word文件转PDF文件已完成!!!")
2. 合并多个Word文件
# 转载来源:https://blog.csdn.net/ayouleyang/article/details/108283956
from os.path import abspath
from win32com import client
import os
wordRoot = "D:\Desktop\Ada\word" #读取word的文件夹
final_docx = r"D:\Desktop\Ada\result\word文件合并结果2.docx"
files = list()
filelist = os.listdir(wordRoot)
for file in filelist:
if (file.endswith(".doc") or file.endswith(".docx")) and ("~$" not in file):
filePath = wordRoot+"\\"+file
files.append(filePath)
# 启动word应用程序
word = client.gencache.EnsureDispatch("Word.Application")
word.Visible = True
# 新建空白文档
new_document = word.Documents.Add()
for fn in files[::-1]:
print ("正在合并:", fn)
fn = abspath(fn)
new_document.Application.Selection.Range.InsertFile(fn)
# 保存最终文件,关闭Word应用程序
new_document.SaveAs(final_docx)
new_document.Close()
word.Quit()
Excel
PowerPoint
1. 合并所有PDF文件
# 转载来源:https://blog.csdn.net/ayouleyang/article/details/108283956
from PyPDF2 import PdfFileMerger
import os
pdfRoot = "D:\Desktop\wordToPDF\pdf" #保存pdf结果的文件夹
merger = PdfFileMerger() #调用PDF文件合并模块
filelist=os.listdir(pdfRoot) #读取文件夹所有文件
for file in filelist:
if file.endswith(".pdf"):
merger.append(pdfRoot+"\\"+file)#合并PDF文件
merger.write("result.pdf") #写入PDF文件
Q.E.D.