PyPandoc 与 PyInstaller 结合使用 [英] PyPandoc in combination with PyInstaller

查看:88
本文介绍了PyPandoc 与 PyInstaller 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了 PyInstaller 来为我的 Python 脚本创建可执行文件,而且效果很好.我使用 PyPandoc 来创建 .docx 报告,当正常的 python 文件运行时它也能正常运行,但不能从 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 are run, but not from the PyInstaller generated executable. It gives the 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.

在创建可执行文件的过程中,我没有看到关于 PyPandoc 的奇怪问题.如何将 Pandoc 包含到我的可执行文件中,以便其他人(没有安装 Python 和/或 Pandoc)可以使用该可执行文件并创建 .docx 报告?

During the executable creation I see no strange issues about PyPandoc. How can I include Pandoc into my executable so others (without Python and/or Pandoc installation) can use the executable and create .docx reports?

一个工作过程包括以下步骤:

edit: a working process included the following steps:

  1. 创建一个包含以下代码的文件:

  1. Create a file including the following code:

import pypandoc
pypandoc.convert(sou‌​rce='# Sample title\nPlaceholder', to='docx', format='md', outputfile='test.doc‌​x')

  • 将此文件另存为 pythonfile.py

    使用 PyInstaller 创建一个可执行文件:

    create an executable with PyInstaller:

    pyinstaller --onefile --clean pythonfile.py
    

  • 现在可执行文件应该在没有安装 Pandoc(或 PyPandoc)的计算机上运行.

  • Now the executable should run on a computer without Pandoc (or PyPandoc) installed.

    推荐答案

    这里有两个问题.第一个是 pypandoc 需要 pandoc.exe 才能工作.pyinstaller 不会自动选择它,但您可以手动指定它.

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

    您可以使用 pyinstaller myspec.spec 构建可执行文件.不要忘记更改路径和 name 参数.

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

    如果您在目录模式下构建它,这应该足够了.但是,对于 one-file 模式,由于 pyinstaller 引导加载程序 有效.pandoc.exe 文件在执行过程中被解压到一个临时文件夹中,但执行发生在你原来的 .exe 文件夹中.根据这个问题,您必须在代码中添加以下几行如果您运行冻结的代码,则调用 pypandoc 来更改您的当前文件夹.

    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.

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

    这篇关于PyPandoc 与 PyInstaller 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆