繁体   English   中英

使用Win32 Python拒绝Word访问

[英]Word access denied with win32 python

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM