AlJohri / docx2pdf

MIT License
513 stars 96 forks source link

AttributeError: 'NoneType' object has no attribute 'SaveAs' #79

Closed lscalese closed 1 year ago

lscalese commented 1 year ago

Hello, I have an error when I try to convert a document. In a Python shell :


import sys
sys.path.append("<install>\Lib\site-packages\win32")
sys.path.append("<install>\Lib\site-packages\win32\lib")
from docx2pdf import convert
convert("<path to>ztest.docx")

I got the error :

  0%|                                                                                            | 0/1 [00:00<?, ?it/s]Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "c:\intersystems\irishealth\mgr\python\docx2pdf\__init__.py", line 106, in convert
    return windows(paths, keep_active)
  File "c:\intersystems\irishealth\mgr\python\docx2pdf\__init__.py", line 33, in windows
    doc.SaveAs(str(pdf_filepath), FileFormat=wdFormatPDF)
AttributeError: 'NoneType' object has no attribute 'SaveAs'

I'm beginner in Python, anyone have idea ? Thank you !

Lorenzo.

ubalklen commented 1 year ago

Do you have MS Word installed?

lscalese commented 1 year ago

Hello @ubalklen , Thank you for your reply. Yes, MS Word is installed. I don't really understand what's wrong. May be a path problem.

lscalese commented 1 year ago

The problem is with these lines :

doc = word.Documents.Open(str(docx_filepath))
print(doc)
# --> None
doc.SaveAs(str(pdf_filepath), FileFormat=wdFormatPDF)

I check the word var :

word = win32com.client.Dispatch("Word.Application")
print(word)
# --> Microsoft Word

The input file exists, If anyone has an idea of the root cause, please leave a comment. Thank you.

ubalklen commented 1 year ago

Is there any special reason to append things to sys.path? I don't think it's related to your issue, but that should not be necessary.

The error log shows you are not being able to open the word file. You can try to reproduce the error more closely using pywin32 directly:

import win32com

word = win32com.client.Dispatch("Word.Application")
doc = word.Documents.Open("<path to>ztest.docx")
doc is None
lscalese commented 1 year ago

Hi @ubalklen ,

Yes, I added this append due to an error module win32api not being found. I'm sure it's a dirty way to fix it, but I'll try to find another way later.

Thank your for your snippet :

import win32com
word = win32com.client.Dispatch("Word.Application")
doc = word.Documents.Open("<path to>ztest.docx")
doc is None

Indeed, I can reproduce the error, doc is None.
Maybe a permission issue, but I don't know why. I will do some tests.

Thank you !

croisez commented 1 year ago

I read here: https://stackoverflow.com/questions/61071022/pywintypes-com-error-2147221008-coinitialize-has-not-been-called-none-n that the win32com client cannot initialize itself in every situation. A solution is to give an argument to the Dispatch() function, like in the following example:

import pythoncom
word = win32com.client.Dispatch("Word.Application", pythoncom.CoInitialize())
nonamesuer commented 1 year ago

Hello,Has this issue been resolved? I also encountered the same error.

lscalese commented 1 year ago

It's solved thank you!.

jussihi commented 7 months ago

@lscalese , How did you solve this issue? I'm having the exact same problem when trying to run docx2pdf as a Windows service.

Could you please tell me how you fixed this?

lscalese commented 7 months ago

Hello @jussihi , unfortunately I don't remember exactly... It was a problem on a student project and I just help him to solve it.

I remember It was a security problem with MS-Word fixed in "Local Security Policy" but "what and why" I don't remember :( unfortunately.

jussihi commented 7 months ago

@lscalese , I was able to fix the problem by following guidance over https://stackoverflow.com/questions/1006923/automating-office-via-windows-service-on-server-2008?rq=4

Thank you anyways :)

lscalese commented 7 months ago

Yes that's it ! Now i see the screen shot I remember.

Glad you fixed your issue :)

croisez commented 7 months ago

IIRC, I had corrected it by adding the argument in bold to the Dispatch function: word = win32com.client.Dispatch("Word.Application", pythoncom.CoInitialize ()) See this page for the hint: https://stackoverflow.com/questions/71292585/python-docx2pdf-attributeerror-open-saveas

Le lun. 26 févr. 2024 à 12:35, Lorenzo Scalese @.***> a écrit :

Yes that's it ! Now i see the screen shot I remember.

Glad you can fix your issue :)

— Reply to this email directly, view it on GitHub https://github.com/AlJohri/docx2pdf/issues/79#issuecomment-1963937261, or unsubscribe https://github.com/notifications/unsubscribe-auth/AANRFSO553F5ZRGENTXRCWLYVRXRNAVCNFSM6AAAAAAVKWUY72VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNRTHEZTOMRWGE . You are receiving this because you commented.Message ID: @.***>