Skip to content Skip to sidebar Skip to footer

Pypandoc In Combination With Pyinstaller

I installed PyInstaller to create executables for my python scripts, and that works fine. I used PyPandoc to create .docx reports, which also run fine when the normal python files

Solution 1:

There are two issues here. The first one is that pypandoc needs pandoc.exe to work. This is not picked up by pyinstaller automatically, but you can specify it manually.

To do this you you have to create a .spec file. The one I generated and used looks like this:

block_cipher = None

a = Analysis(['pythonfile.py'],
             pathex=['CodeDIR'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='EXEName',
          debug=False,
          strip=False,
          upx=True,
          console=True , 
          resources=['YourPandocLocationHere\\\\pandoc.exe'])

You can build the executable by using pyinstaller myspec.spec. Don't forget to change the paths and the name parameter.

If you were building that in a directory mode this should be enough. However, for the one-file mode, things are a bit more complicated due to the way the pyinstaller bootloader process works. The pandoc.exe file is unzipped during execution in a temporary folder, but the execution happens in your original .exe folder. According to this question, you have to add the following lines to your code before calling pypandoc to change your current folder, if you run the frozen code.

ifhasattr(sys, '_MEIPASS'):
    os.chdir(sys._MEIPASS)

Post a Comment for "Pypandoc In Combination With Pyinstaller"