Search code examples
pythonpythoncom

'NoneType' object has no attribute 'SaveAs' docx2pdf on windows


from docx2pdf import convert
import pythoncom 

#flask rest api
@app.route('/api_v1/get_pdf/',methods=['GET'])
def get_pdf():
    if threading.currentThread ().getName () != 'MainThread':
        pythoncom.CoInitialize () 
    convert(in_word,out_pdf )

I'm using docx2pdf to convert docx file to pdf in python, my script needs to execute when windows start( without login to windows ). in this mode the script raises error when converting to pdf 'NoneType' object has no attribute 'SaveAs'. i don't know where the problem is, but i think it happend because some services not working before login to windows, but if i run the script after login to windows, works correctly. So how can i make the script works before login to windows?

Traceback:

'NoneType' object has no attribute 'SaveAs'
Traceback (most recent call last):
File "D:\xxxx\main.py", line 746, in get_pdf
convert(in_word,out_pdf )
File "C:\Python38\lib\site-packages\docx2pdf\__init__.py", line 106, in convert
return windows(paths, keep_active)
File "C:\Python38\lib\site-packages\docx2pdf\__init__.py", line 33, in windows
doc.SaveAs(str(pdf_filepath), FileFormat=wdFormatPDF)
AttributeError: 'NoneType' object has no attribute 'SaveAs'

Solution

  • To do this kind of actions unattended you need to go to the COM services of your computer. Select the application you want to run unattended, in this case WORD. In the options select to run with the user and password that you introduce and save it.

    Once you do that those applications will run as that user and that password.

    Be careful when using this as now those applications can be use unattended and might have security breaches.

    Hope it helps.