【问题标题】:Can't run app exe on other machines, python2.7, fpdf, pyinstaller不能在其他机器上运行app exe,python2.7,fpdf,pyinstaller
【发布时间】:2017-10-24 08:46:43
【问题描述】:

我在其他机器上从 pyinstaller 运行 exe 应用程序时遇到问题。它正在寻找我构建应用程序的 PC 上的路径:

console output

我通过以下方式在 pyPDF 中使用和添加字体:

from fpdf import FPDF

pwd = os.path.realpath(os.path.dirname(sys.argv[0])) + "\\font\\DejaVuSansCondensed.ttf"

pdf = FPDF(orientation = 'L', unit = 'mm', format='A4')
pdf.add_page()
# Add a DejaVu Unicode font (uses UTF-8)
# Supports more than 200 languages. For a coverage status see:
# http://dejavu.svn.sourceforge.net/viewvc/dejavu/trunk/dejavu-fonts/langcover.txt
pdf.add_font('DejaVu', '', pwd, uni=True)
pdf.set_font('DejaVu', '', 18)
#then I use pdf.write() to write data
#save and close pdf file
pdf.output('C:\\Users\\' + getpass.getuser() + '\\Documents\\pdf_file' + time_stamp + '.pdf', 'F')

我尝试通过以下方式构建它:

pyinstaller app.py
pyinstaller --onefile app.py

在我构建代码的机器上没有问题。我想 fpdf 的输出方法或 pyinstaller 的设置中有些东西,对吗?

我必须用 unicode 字符创建 pdf。我使用最新版本的 fpdf 和 pyinstaller 模块。

我将不胜感激。

提前谢谢你,

【问题讨论】:

    标签: python-2.7 pyinstaller fpdf


    【解决方案1】:

    当你在你的机器上构建它时没有问题,因为你有 DejaVuSansCondensed.ttf 在 python 期望它的位置。但是当您使用 PyInstaller 编译并在另一个系统上运行它时,它会在同一个位置查找(其他系统上可能不存在)。

    我的建议是将该 ttf 文件复制到您当前的工作目录并将行字体行更新为(或适合您代码的类似内容):

    pwd = "DejaVuSansCondensed.ttf"
    

    您还必须确保 .exe 可以访问该文件,独立于系统(相对于 exe)。所以你必须将ttf文件复制到你的exe所在的目录,这样当你在另一台计算机上运行它时,你的代码会在exe所在的目录中查找ttf文件。

    【讨论】:

      猜你喜欢
      • 2018-06-29
      • 2021-03-01
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多