.doc 到 pdf 使用 python [英] .doc to pdf using python
问题描述
我的任务是将大量 .doc 文件转换为 .pdf.我的主管希望我这样做的唯一方法是通过 MSWord 2010.我知道我应该能够使用 python COM 自动化来实现自动化.唯一的问题是我不知道如何以及从哪里开始.我尝试搜索一些教程,但找不到任何教程(可能我有,但我不知道我在找什么).
I'am tasked with converting tons of .doc files to .pdf. And the only way my supervisor wants me to do this is through MSWord 2010. I know I should be able to automate this with python COM automation. Only problem is I dont know how and where to start. I tried searching for some tutorials but was not able to find any (May be I might have, but I don't know what I'm looking for).
现在我正在阅读这个.不知道这会有多大用处.
Right now I'm reading through this. Dont know how useful this is going to be.
推荐答案
一个使用 comtypes,转换单个文件,输入和输出文件名作为命令行参数:
A simple example using comtypes, converting a single file, input and output filenames given as commandline arguments:
import sys
import os
import comtypes.client
wdFormatPDF = 17
in_file = os.path.abspath(sys.argv[1])
out_file = os.path.abspath(sys.argv[2])
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
您也可以使用 pywin32,除了:
You could also use pywin32, which would be the same except for:
import win32com.client
然后:
word = win32com.client.Dispatch('Word.Application')
这篇关于.doc 到 pdf 使用 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!