.DOC到PDF使用python [英] .doc to pdf using python
问题描述
我'装吨.doc文件转换为pdf任务。和我的上司要我做到这一点的唯一方法是通过2010年的MSWord我知道我应该能够与蟒蛇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屋!