0
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'
Mehrdad Dadvand
  • 340
  • 4
  • 19
  • Do you have any more information on the error, like maybe a stack trace? Where are you seeing the error? How are you causing the script to be run when Windows starts? – CryptoFool Feb 26 '21 at 06:52
  • i saved the Exceptions message into file, when i open the log file, i saw `'NoneType' object has no attribute 'SaveAs'` and i realized the `convert` function is not working. thats all i know – Mehrdad Dadvand Feb 26 '21 at 06:57
  • How did you save the exception message to a file? If you're saying that you wrote code inside a `catch` block to write the exception message to a file, try using these lines to get more info into the file: `f.write(str(e))`, `f.write(traceback.format_exc())` – CryptoFool Feb 26 '21 at 07:09

1 Answers1

0

docx2pdf does the conversion by running the Microsoft Word application. Word cannot start if there is no user logged in, even if it won't display a UI. You will have to find another docx converter that doesn't use Word. There are several options, including pandoc.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • The point is i use `docx` to edit the word file before i convert to pdf, but `docx` works correctly. does `docx` uses the same method? so why works? – Mehrdad Dadvand Feb 26 '21 at 07:22
  • No. The Python `docx` module edits the contents directly. It does not invoke Microsoft Word. – Tim Roberts Feb 26 '21 at 18:38