Search code examples
pythonpyinstallerexetqdmdocx2pdf

Error with docx2pdf after compiling using pyinstaller


I compiled a script (running smoothly) that I made in python using Pyinstaller, but the function that converts from word to pdf is stopping the code.

The error:

File "docx2pdf\__init__.py", line 106, in convert
  File "docx2pdf\__init__.py", line 29, in windows
  File "tqdm\asyncio.py", line 24, in __init__
  File "tqdm\std.py", line 1109, in __init__
  File "tqdm\std.py", line 1361, in refresh
  File "tqdm\std.py", line 1509, in display
  File "tqdm\std.py", line 350, in print_status
  File "tqdm\std.py", line 343, in fp_write
  File "tqdm\utils.py", line 89, in __getattr__
AttributeError: 'NoneType' object has no attribute 'write'

I already tried to reinstall the packages and recompile.

I already put all the functions in a file just in case and compile again, but the error didn't change.


Solution

  • I ran into this same issue yesterday actually and it took me a bit to figure out.

    It turns out docx2pdf uses tqdm to display a progress bar in the console. To do this, it writes to stderr to the console. When I used PyInstaller to create a windowed application without a console, I got this error message as well.

    Redirecting stderr for the docx2pdf parts worked for me like the below:

    import sys 
    import docx2pdf 
    
    sys.stderr = open("consoleoutput.log", "w")
    
    convert('your file here')
    
    

    I'm sure there are more elegant, thread-safe solutions on here if you look around though. Hope this helps and Good Luck!