PyPandoc in combination with PyInstaller

I installed PyInstaller to create executables for my python scripts and it works fine. I used PyPandoc to create reports .docxthat also work fine when running regular python files, but not from the PyInstaller executable. It gives an error:

Traceback (most recent call last):
  File "src\flexmodel_postcalc.py", line 295, in postcalculate_everything
  File "src\flexmodel_postcalc.py", line 281, in generate_report_docx
  File "src\flexmodel_report_docx.py", line 118, in generate_text_useages_docx
  File "pypandoc\__init__.py", line 50, in convert
  File "pypandoc\__init__.py", line 70, in _convert
  File "pypandoc\__init__.py", line 197, in get_pandoc_formats
  File "pypandoc\__init__.py", line 336, in _ensure_pandoc_path
OSError: No pandoc was found: either install pandoc and add it
to your PATH or install pypandoc wheels with included pandoc.

While creating the executable, I do not see any strange problems in PyPandoc. How do I include Pandoc in my executable so that others (without installing Python and / or Pandoc) can use the executable and create reports .docx?

edit: the workflow included the following steps:

  • Create a file containing the following code:

    import pypandoc
    pypandoc.convert(sou‌​rce='# Sample title\nPlaceholder', to='docx', format='md', outputfile='test.doc‌​x')
    
  • Save this file as pythonfile.py

  • create an executable file with PyInstaller:

    pyinstaller --onefile --clean pythonfile.py
    
  • Pandoc ( PyPandoc).

+2
1

. , pypandoc pandoc.exe. pyinstaller, .

.spec. , , :

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'])

, pyinstaller myspec.spec. name.

, . one-file - , pyinstaller . pandoc.exe , .exe. , , pypandoc , .

if hasattr(sys, '_MEIPASS'):
    os.chdir(sys._MEIPASS)
+3

Source: https://habr.com/ru/post/1651256/


All Articles