'Python docx2pdf AttributeError: Open.SaveAs
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
Solution 1:[1]
from docx2pdf import convert
inputFile = "document.docx"
outputFile = "document2.pdf"
file = open(outputFile, "w")
file.close()
convert(inputFile, outputFile)
You should create the output file first
Solution 2:[2]
change:
word = win32com.client.Dispatch('Word.Application')
to
import pythoncom
word = win32com.client.Dispatch('Word.Application', pythoncom.CoInitialize())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | wl9724 |
Solution 2 | Jeril |