Python PyPandoc与PyInstaller的结合
我安装了PyInstaller来为我的python脚本创建可执行文件,这很好。我使用PyPandoc创建.docx报告,当运行普通python文件时,该报告也可以正常运行,但不是从PyInstaller生成的可执行文件运行。它给出了错误:Python PyPandoc与PyInstaller的结合,python,pyinstaller,pandoc,pypandoc,Python,Pyinstaller,Pandoc,Pypandoc,我安装了PyInstaller来为我的python脚本创建可执行文件,这很好。我使用PyPandoc创建.docx报告,当运行普通python文件时,该报告也可以正常运行,但不是从PyInstaller生成的可执行文件运行。它给出了错误: Traceback (most recent call last): File "src\flexmodel_postcalc.py", line 295, in postcalculate_everything File "src\flexmodel
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报告
编辑:工作流程包括以下步骤:
创建包含以下代码的文件:
import pypandoc
pypandoc.convert(source='# Sample title\nPlaceholder', to='docx', format='md', outputfile='test.docx')
将此文件另存为pythonfile.py
使用PyInstaller创建可执行文件:
pyinstaller --onefile --clean pythonfile.py
现在,可执行文件应该在未安装Pandoc或PyPandoc的计算机上运行
这里有两个问题。第一个是pypandoc需要pandoc.exe才能工作。pyinstaller不会自动获取此信息,但您可以手动指定 要做到这一点,你必须这样做。我生成并使用的一个如下所示:
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参数
如果您是在目录模式下构建的,这就足够了。然而,对于单文件模式,由于pyinstaller的工作方式,事情要复杂一些。pandoc.exe文件在临时文件夹中执行时会被解压缩,但执行会在原始的.exe文件夹中进行。根据,如果运行冻结的代码,则在调用pypandoc更改当前文件夹之前,必须在代码中添加以下行
if hasattr(sys, '_MEIPASS'):
os.chdir(sys._MEIPASS)
您能否添加一个python脚本的最小工作示例,以查看您使用的是pandoc的哪一部分?我只尝试了pypandoc,在我的windows系统中似乎工作得很好。很可能您需要添加pypandoc作为一个加上一些其他依赖项。@Repiklis我可以给出一个非常简短的示例代码,但现在不能测试它。进口pypandoc;pypandoc.convertsource='Sample title\nPlaceholder',to='docx',format='md',outputfile='test.docx'。这保存在一个.py文件中,我使用PyInstaller命令PyInstaller-onefile-clean-p H:\AppData\Python\Python27\Lib\site packages pythonfile.py将其转换为PyInstaller。请在问题中发布示例代码,以便其他人理解您的问题。“我会在下面贴一个答案。”雷皮克利斯补充道。提前谢谢!谢谢你发布这个。由于某种原因,我没有让-p论点起作用,但至少我走上了正确的道路。谢谢您必须在.spec文件的末尾添加一个,才能编译为one-dir。resources参数不应为列表是否正确?我用is得到一个TypeError,没有[]它就可以工作。为了将来参考,我在pathex参数中包含了-p参数文件夹,并在该列表中添加了一个新项。