【问题标题】:Why does pyinstaller create a huge exe file when using it with pdfminer modules in Python 3.6?为什么 pyinstaller 在 Python 3.6 中与 pdfminer 模块一起使用时会创建一个巨大的 exe 文件?
【发布时间】:2020-05-01 23:24:04
【问题描述】:

我尝试使用 pyinstaller(在 Python 3.6 中)从使用 pdfminer 模块的脚本创建一个 exe 文件,但创建的 exe 文件很大,大约 240 MB。相比之下,在 Python 2.7 中使用带有类似脚本的 pyinstaller 时,创建的 exe 文件只有 10 MB 左右。

我做错了什么?

我使用以下命令创建 exe 文件:pyinstaller.exe --onefile {filename/path}

我的代码:

from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage

...

def convert_pdf_to_txt(path):
    rsrcmgr = PDFResourceManager()
    retstr = io.StringIO()
    #codec = 'windows-1250'
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, laparams=laparams)
    fp = open(path, 'rb')
    # reply = s.get(path, stream=True, verify= False)
    # fp = StringIO()
    # fp.write(reply.content)
    # fp.seek(0)
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    password = ""
    maxpages = 0
    caching = True
    pagenos = set()

    for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password, caching=caching, check_extractable=True):
        interpreter.process_page(page)

    text = retstr.getvalue()

    fp.close()
    device.close()
    retstr.close()
    return text

...

【问题讨论】:

  • 我建议禁用 onefile 选项以调查是否存在捆绑作为输出的一部分的剩余输出文件。可能有模板等文件未被用作输出的一部分。您可能需要使用 pyinstaller 规范文件从输出中专门删除这些内容。当我尝试时,pyinstaller 将您的源捆绑为 12mb。

标签: pyinstaller pdfminer


【解决方案1】:

我找到了以下解决方案:

  1. 创建了一个虚拟环境
  2. 通过 pip 仅安装所需的包(即 pdfminer 和 pyinstaller)。

现在生成的 exe 文件只有 7.8 MB。

【讨论】:

    猜你喜欢
    • 2017-03-20
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    相关资源
    最近更新 更多