Python docx2pdf AttributeError: Open.SaveAs

3k Views Asked by At

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

3

There are 3 best solutions below

1
wl9724 On
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

1
Fluid Power and Controls On

change:

word = win32com.client.Dispatch('Word.Application')

to

import pythoncom
word = win32com.client.Dispatch('Word.Application', pythoncom.CoInitialize())
0
Arjun Viswanathan On

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.