使用win32 python拒绝Word访问

时间:2018-05-17 13:07:23

标签: python django

我使用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)

我感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这应该有效:

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()