用pyinstaller打包,exe报错

为了练习python的tkinter,我用pdf2docx写了一个批量把pdf转为docx文件的代码。在Pycharm中运行正常。然后我用pyinstaller将该py文件打包,打包过程也很顺利。可是最后打开exe文件时,它就弹出了错误信息的窗口。
这是主程序代码:

# coding:utf-8

from pdf2docx import Converter
import os
import tkinter as tk
from tkinter import filedialog
import sys
import threading



class StdoutRedirector:
    def __init__(self, text_widget):
        self.text_widget = text_widget

    def write(self, text):
        self.text_widget.insert(tk.END, text)
        self.text_widget.see(tk.END)  # 滚动到文本末尾

    def flush(self):
        pass

def convert_pdf_to_word(pdf_path, word_path):
    sys.stdout = StdoutRedirector(tx0)  # 重定向输出
    try:
        cv = Converter(pdf_path)
        cv.convert(word_path, start=0, end=None)
        cv.close()
        print("转换完成!")
    except Exception as e:
        print(f"转换失败:{e}")
    finally:
        sys.stdout = sys.__stdout__  # 恢复标准输出

def pdfs_to_words(pdf_dir, word_dir):
    tx0.delete(0.0, "end")
    print("开始转换……{}\n".format(pdf_dir))
    for root, dirs, files in os.walk(pdf_dir):
        for file in files:
            if file[-3:] == "pdf":
                pdf_path = os.path.join(root, file)
                word_path = os.path.join(word_dir, file.replace(".pdf", ".docx"))
                print("正在转换:{}……\n".format(file))
                threading.Thread(target=convert_pdf_to_word, args=(pdf_path, word_path)).start()
                print("转换成功!\n")

def get_path(x):
    path = filedialog.askdirectory()
    x.delete(0.0, "end")
    x.insert("end", path)

def pdf_to_word_button():
    # 注意把get到的路径前后的空字符(主要是后)清除
    input_path = tx1.get(0.0, "end").strip()
    output_path = tx2.get(0.0, "end").strip()
    pdfs_to_words(input_path, output_path)

window = tk.Tk()
window.title("PDF转换为Word工具")
window.geometry("500x500")

window.bind("<Escape>", lambda x:window.quit())

l1 = tk.Label(window, text="请输入PDF文件所在文件夹")
l1.place(x=5, height=20)
tx1 = tk.Text(window)
tx1.place(x=5, y=25, width=405, height=20)
b1 = tk.Button(window, text="浏览", command=lambda:get_path(tx1))
b1.place(x=415, y=25, width=80, height=20)

l2 = tk.Label(window, text="请输入Word文件需保存在的文件夹")
l2.place(x=5, y=50,height=20)
tx2 = tk.Text(window)
tx2.place(x=5, y=75, width=405, height=20)
b2 = tk.Button(window, text="浏览", command=lambda:get_path(tx2))
b2.place(x=415, y=75, width=80, height=20)

b3 = tk.Button(window, text="开始转换", command=pdf_to_word_button)
b3.place(x=210, y=100, width=80, height=20)

tx0 = tk.Text(window)
tx0.place(x=5, y=125, width=490, height=370)

window.mainloop()

这是用pyinstaller打包的命令:
pyinstaller -F -w --hidden-import=pdf2docx test.py

这是点击exe文件弹出的窗口:

img

打包是没问题的,你确认下你pycharm的python环境,和执行pyinstaller打包的python环境是否是一个。
是不是pycharm启用了虚拟环境,pdf2docx 只安装在了虚拟环境里,pyinstaller所在的环境里没有安装pdf2docx

因为你相关依赖包没有打进去 导致找不到相关包所以报错了

可以考虑把pdf2docx添加到打包生成的.spec文件hiddenimports中,保存后通过该.spec文件再进行打包
pyinstaller .spec
或直接再pyinstaller打包命令中添加--hidden-import=pdf2docx

pdf2docx包没打进去

问题点:打包后工具提示模块缺失
分析思路:
①有可能是运行环境和打包环境不一致,确认一下打包环境,并更新缺失的模块(推荐)
②打包语句错误
试试这个

pyinstaller -F -w test_db.py
不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7775226
  • 除此之外, 这篇博客: 小说下载器exe-python tkinter打包‘笔趣阁爬虫’为exe文件中的 2,pyinstaller 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:

    对程序打包会使用到这个库,PyInstaller是python的第三方库,其重要作用是将.py源代码转换成无需源代码的可执行文件,即
    .py----PyInstaller----Windows(exe文件)、Linux、Mac OS X
    执行命令:
    简单理解,前面是生成exe的图标路径,后面是py文件路径。 .ico是图像类型

    E:\a-pycharm\爬虫学习>pyinstaller -F -w -i x:\xx\xxx\xxx\x.ico x:\xxx\xxx\xxxx\xxx.py
    

    使用说明:
    1,首先在搜索框输入你需要的小说名称,点击搜索后,会打印搜索的内容。
    2,路径是可以不选择的,默认会下载在与该exe的同级目录下。
    3,鼠标点击你需要的小说,会在搜索内容后面打印出来。
    4,最后点击下载即可开始。

    !!!!!!!!!!!!!!!!!!最后下载时调用的函数是最复杂的这里还包含爬取免费IP的过程,程序会出现短暂的未响应状态,在开始爬取的时候会恢复正常,这时会打印下载进度。
    在这里插入图片描述
    这里怕侵权小说作者权益,所以全部内容都打了码,怕审核不过。
    在这里插入图片描述


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^