我使用 Python 3.6 和 Django 1.11。我用 MailMerge 创建了一个 Word 文档,没问题。现在我需要将此文档另存为 pdf 文档。
import win32com.client as win32
from os import path
word = win32.DispatchEx("Word.Application")
filedoc='c:\\growthtech\\Capturar6.docx'
filepdf='c:\\growthtech\\Capturar6.pdf'
in_file = path.abspath(filedoc)
out_file = path.abspath(filepdf)
doc = word.Documents.Open(in_file, 'rb')
doc.SaveAs(new_file, FileFormat=17)
doc.Close()
word.Quit()
它在“doc = word.Documents.Open(in_file, 'rb')”行中发生了错误。
>>> doc = word.Documents.Open(in_file, 'rb')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352571, 'Tipo não correspondente.', None, 2)
我很感激任何帮助。
这应该有效:
import win32com.client as win32
from os import path
word = win32.DispatchEx("Word.Application")
in_file = path.abspath('c:\\growthtech\\Capturar6.docx')
out_file = path.abspath('c:\\growthtech\\Capturar6.pdf')
# just one argument here
doc = word.Documents.Open(in_file)
# was: 'new_file'
doc.SaveAs(out_file, FileFormat=17)
doc.Close()
word.Quit()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句