Python未报错也可正常运行,但用pyinstaller打包时,却提示语法错误

import os
import openpyxl
import pandas as pd
import pdfplumber
import tkinter
from tkinter import filedialog

from tkinter import messagebox

root = tkinter.Tk()
root.title(‘T77-PDF提取器’)
max_w, max_h = root.maxsize()
root.geometry(f’500x300+{int((max_w - 500) / 2)}+{int((max_h - 300) / 2)}’) # 居中显示
root.resizable(width=False, height=False)

标签组件

label = tkinter.Label(root, text=’选择目录:’, font=(‘FangSong’, 12))
label.place(x=60, y=100)

输入框控件

entry_text = tkinter.StringVar()
entry = tkinter.Entry(root, textvariable=entry_text, font=(‘FangSong’, 10), width=30, state=’readonly’)
entry.place(x=150, y=105)

按钮控件

def get_path():
“””注意,以下列出的方法都是返回字符串而不是数据流”””
global file_path

# 返回一个字符串,且只能获取文件夹路径,不能获取文件的路径。

file_path = filedialog.askdirectory(title=’请选择一个目录’)

# 返回一个字符串,可以获取到任意文件的路径。

file_path = filedialog.askopenfilename(title=’请选择文件’)

生成保存文件的对话框, 选择的是一个文件而不是一个文件夹,返回一个字符串。

file_path = filedialog.asksaveasfilename(title=’请输入保存的路径’)

entry_text.set(file_path)
return file_path

button = tkinter.Button(root, text=’选择路径’, command=get_path)

button.place(x=400, y=95)

def split_pdf():
path = file_path
pd.set_option(‘display.max_columns’, None) # 显示所有列
pd.set_option(‘display.max_rows’, None) # 显示所有行
all_table = pd.DataFrame()
for i in os.listdir(file_path): # 获取filePath路径下所有文件名
data_collect = ‘’.join(i)
tables = list() # 文件名字符串格式
pdf = pdfplumber.open(path + “\“ + data_collect)
for p in range(1, 3):
page = pdf.pages[p] # 指定页面数
table = page.extract_table()
tables.extend(table)
df = pd.DataFrame(tables)
df = df.replace(‘\n’, ‘’, regex=True) # 替换换行符
pdf_row = df.shape[0] # 计算行数
h = list()
i = 1
while i < pdf_row - 1:
xh = str(df.iat[i, 0]) # 表格指定位置
xh2 = str(df.iat[i, 1]) # 表格指定位置
if xh[0: 2] != ‘1/‘ and xh[0: 2] != ‘2/‘ and xh[0: 2] != ‘3/‘:
h.append(i) # 将i加入list
if len(xh2) > 5:
h.append(i) # 将i加入list
if xh is None:
h.append(i) # 将i加入list
i = i + 1
df = df.drop(h) # 删除指定行
df = df.drop(pdf_row - 1)
df = df.rename(columns=df.iloc[0]).drop(df.index[0])

    new_df = df[['VENDOR STYLE', 'FIRST COST', 'UNITS', 'PACK QTY', 'NST CODE', 'MST CTN QTY']]
    pdf_name1 = data_collect[:35:-1]
    pdf_name2 = pdf_name1[0:6]
    PO = pdf_name2
    new_df.insert(0, "PO", PO)  # 插入一列“PO

all_table = pd.concat([all_table, new_df], ignore_index=True)

os.chdir(path)
wb = openpyxl.Workbook()
wb.save('SC汇总表.xlsx')
all_table.to_excel(path + "\\SC汇总表.xlsx", header=True, index=False)
# messagebox.showinfo("information", "已生成excel文件!若需重新操作,请删除已有excel文件!")

button = tkinter.Button(root, text=’生成EXCEL’, command=split_pdf)

button.place(x=400, y=135)

root.mainloop()

报错如下:
SyntaxError: invalid syntax
Traceback (most recent call last):
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\runpy.py”, line 197, in run_module_as_main
return run_code(code, main_globals, None,
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\runpy.py”, line 87, in _run_code
exec(code, run_globals)
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\Scripts\pyinstaller.exe__main
.py”, line 7, in
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller_main_.py”, line 178, in run
run_build(pyi_config, spec_file, *vars(args))
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller_main_.py”, line 59, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, *
kwargs)
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\building\build_main.py”, line 934, in main
build(specfile, distpath, workpath, clean_build)
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\building\build_main.py”, line 856, in build
exec(code, spec_namespace)
File “D:\python\venv\Lib\site-packages\PyInstaller\T77-合同提取器1.3.spec”, line 7, in
a = Analysis(
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\building\build_main.py”, line 381, in init
self.postinit()
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\building\datastruct.py”, line 173, in postinit
self.assemble()
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\building\build_main.py”, line 550, in assemble
self.graph.process_post_graph_hooks(self)
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\depend\analysis.py”, line 329, in process_post_graph_hooks
module_hook.post_graph(analysis)
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\depend\imphook.py”, line 429, in post_graph
self._load_hook_module()
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\depend\imphook.py”, line 384, in _load_hook_module
self._hook_module = importlib_load_source(self.hook_module_name, self.hook_filename)
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\compat.py”, line 603, in importlib_load_source
return mod_loader.load_module()
File ““, line 529, in _check_name_wrapper
File ““, line 1029, in load_module
File ““, line 854, in load_module
File ““, line 274, in _load_module_shim
File ““, line 711, in _load
File ““, line 680, in _load_unlocked
File ““, line 850, in exec_module
File ““, line 228, in _call_with_frames_removed
File “C:\Users\hyt\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\hooks\hook-sqlalchemy.py”, line 31, in
dialects = eval(dialects.strip())
File ““, line 0

讨论数量: 2
Jason990420
  1. 代码格式错误, 缩格不对, 请参考以下内容, 再重新编辑内容.

代码高亮

```python

你的代码

```

  1. 报错讯息好像不完整
  2. 如何使用 pyinstaller 打包的, 还有是什么平台, 版本为何 ?
1年前 评论

我建议你使用JetBrains的pthon编辑器,扫描下项目代码

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!