Python docx2pdf 属性错误:Open.SaveAs
我正在尝试使用 docx2pdf 库将 docx 文件转换为 pdf,使用以下代码:
from docx2pdf import convert
convert("generated.docx")
Traceback (most recent call last):
File "c:\Users\user\Desktop\folder\script.py", line 29, in <module>
convert("generated.docx")
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\docx2pdf-0.1.8-py3.10.egg\docx2pdf\__init__.py", line 106, in convert
return windows(paths, keep_active)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\docx2pdf-0.1.8-py3.10.egg\docx2pdf\__init__.py", line 33, in windows
doc.SaveAs(str(pdf_filepath), FileFormat=wdFormatPDF)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py", line 639, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Open.SaveAs
我也尝试使用 comtypes 和 pywin32 进行转换,但出现了相同的错误。我从此处获取代码。
import sys
import comtypes.client
wdFormatPDF = 17
in_file = os.path.abspath("generated.docx")
out_file = os.path.abspath("generated.pdf")
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
---------------------------------
Traceback (most recent call last):
File "c:\Users\user\Desktop\folder\script.py", line 45, in <module>
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
_ctypes.COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))
import sys
import win32com.client
wdFormatPDF = 17
in_file = os.path.abspath("generated.docx")
out_file = os.path.abspath("generated.pdf")
word = win32com.client.Dispatch('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
---------------------------------
Traceback (most recent call last):
File "c:\Users\user\Desktop\folder\script.py", line 46, in <module>
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py", line 639, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Open.SaveAs
我该如何修复这个错误?或者请建议另一种将 docx 转换为 pdf 的方法。先感谢您
I am trying to convert a docx file to pdf using the docx2pdf library, using the following code:
from docx2pdf import convert
convert("generated.docx")
As written here. But I have an error:
Traceback (most recent call last):
File "c:\Users\user\Desktop\folder\script.py", line 29, in <module>
convert("generated.docx")
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\docx2pdf-0.1.8-py3.10.egg\docx2pdf\__init__.py", line 106, in convert
return windows(paths, keep_active)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\docx2pdf-0.1.8-py3.10.egg\docx2pdf\__init__.py", line 33, in windows
doc.SaveAs(str(pdf_filepath), FileFormat=wdFormatPDF)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py", line 639, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Open.SaveAs
I also tried converting with comtypes and pywin32, but I get the same error. I take code from here.
import sys
import comtypes.client
wdFormatPDF = 17
in_file = os.path.abspath("generated.docx")
out_file = os.path.abspath("generated.pdf")
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
---------------------------------
Traceback (most recent call last):
File "c:\Users\user\Desktop\folder\script.py", line 45, in <module>
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
_ctypes.COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))
import sys
import win32com.client
wdFormatPDF = 17
in_file = os.path.abspath("generated.docx")
out_file = os.path.abspath("generated.pdf")
word = win32com.client.Dispatch('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
---------------------------------
Traceback (most recent call last):
File "c:\Users\user\Desktop\folder\script.py", line 46, in <module>
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py", line 639, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Open.SaveAs
How can I fix this error? Or please suggest another way to convert docx to pdf. Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
改变:
到
change:
to
您应该首先创建输出文件
You should create the output file first
我的观察之一是,当在 Microsoft Word 中打开 Word 文档,然后我们尝试对同一个 Word 文件执行
convert()
时,就会发生此问题。如果是这种情况,如果文件已打开,请先将其关闭,然后重试。
One of the observations I had is that this issue happens when the Word document is opened in Microsoft Word and then we try to execute
convert()
for the same Word file.If that is the case, if the file is opened, please close it first and try again.