Python实现批量Word转PDF
程序员文章站
2022-04-02 11:02:27
Python实现批量Word转PDF先说一下应用场景,投标需要在标书上传的时候上传PDF版本,写完Word后一个一个另存为比较麻烦,所以想用这种批量化的方式,一键转换,功能很简单,代码也是借鉴https://blog.csdn.net/dingjianmin/article/details/103315562?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160741527119721940243792%2522%252C%2522scm...
Python实现批量Word转PDF
先说一下应用场景,投标需要在标书上传的时候上传PDF版本,写完Word后一个一个另存为比较麻烦,所以想用这种批量化的方式,一键转换,功能很简单,代码也是借鉴https://blog.csdn.net/dingjianmin/article/details/103315562?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160741527119721940243792%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=160741527119721940243792&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-1-103315562.pc_search_result_cache&utm_term=word%E8%BD%ACpdf&spm=1018.2118.3001.4449
简单介绍使用方法,修改代码中的放Word文件的文件夹地址,手动在文件夹中新建一个名为“PDF”的文件夹,程序即可正常运行。
from win32com.client import Dispatch
from os import walk
path = 'C:\\Users\\Administrator\\Desktop\\wordtest'
pdfpath = str(path+'\\'+'PDF')#PDF文件夹地址,就在Word文件夹内
def wordtopdf(exfile):#输入数据是单个文件的路径
word = Dispatch('Word.application')#启动Word程序
doc = word.Documents.Open(exfile)#获取一个Word对象
pdfname = exfile.split('\\')[-1]
pdfname = pdfname.replace('.docx','.pdf')#设置PDF文件的命名
doc.SaveAs(str(pdfpath+'\\'+pdfname),FileFormat = 17)#设定保存的文件类型,SaveAs支持的保存类型有多种,通过设置FileFormat参数决定,参数数值代表的文件类型见下方
doc.Close()
word.Quit()
print(str('已转换'+pdfname))
if __name__ == '__main__':
for filepath,manuname,filename in walk(path):#walk函数的三个返回值分别是:文件地址;目录名称;目录下的文件名称列表
print(filename)
for file in filename:
if file.endswith('.docx'):#判断是否是Word文件,我这里转换的都是以.docx结尾
wordtopdf(str(path+'\\'+file))#输入单个文件路径
wdFormatDocument = 0
wdFormatDocument97 = 0
wdFormatDocumentDefault = 16
wdFormatDOSText = 4
wdFormatDOSTextLineBreaks = 5
wdFormatEncodedText = 7
wdFormatFilteredHTML = 10
wdFormatFlatXML = 19
wdFormatFlatXMLMacroEnabled = 20
wdFormatFlatXMLTemplate = 21
wdFormatFlatXMLTemplateMacroEnabled = 22
wdFormatHTML = 8
wdFormatPDF = 17
wdFormatRTF = 6
wdFormatTemplate = 1
wdFormatTemplate97 = 1
wdFormatText = 2
wdFormatTextLineBreaks = 3
wdFormatUnicodeText = 7
wdFormatWebArchive = 9
wdFormatXML = 11
wdFormatXMLDocument = 12
wdFormatXMLDocumentMacroEnabled = 13
wdFormatXMLTemplate = 14
wdFormatXMLTemplateMacroEnabled = 15
wdFormatXPS = 18
本文地址:https://blog.csdn.net/kengmila9393/article/details/110875719