Repository navigation
Expand file tree
/
Copy pathbuild_script.py
More file actions
125 lines (111 loc) · 4.89 KB
/
Copy pathbuild_script.py
File metadata and controls
125 lines (111 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import sys
import os
import re
import PyInstaller.__main__
# 20261022 151500 修改:自动从 main_app.py 读取版本号和构建日期,不再人工维护
def load_version_info():
"""从 main_app.py 自动解析 APP_VERSION 和 APP_BUILD_DATE"""
main_app_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'main_app.py')
if not os.path.exists(main_app_path):
print(f"❌ 错误:找不到 main_app.py 文件(用于读取版本信息)")
sys.exit(1)
try:
with open(main_app_path, 'r', encoding='utf-8') as f:
content = f.read()
# 解析 APP_VERSION,匹配形如 APP_VERSION = "V1.6"
version_match = re.search(r'APP_VERSION\s*=\s*["\']([^"\']+)["\']', content)
build_match = re.search(r'APP_BUILD_DATE\s*=\s*["\']([^"\']+)["\']', content)
if not version_match or not build_match:
print("❌ 错误:无法从 main_app.py 解析版本号或构建日期")
sys.exit(1)
return version_match.group(1), build_match.group(1)
except Exception as e:
print(f"❌ 错误:读取 main_app.py 版本信息失败:{e}")
sys.exit(1)
APP_VERSION, BUILD_DATE = load_version_info() # 20261022 151500 修改:自动读取,不再硬编码
BUILD_NAME = f"DirCopyTool_{BUILD_DATE}_{APP_VERSION}"
def check_dependencies():
"""检查必要的依赖库是否已安装"""
try:
import docx
print("✅ python-docx 库已安装")
except ImportError:
print("⚠️ 警告:python-docx 库未安装,DOCX导出功能可能无法正常工作")
print(" 请运行: pip install python-docx>=0.8.11")
response = input("是否继续打包?(y/n): ")
if response.lower() != 'y':
sys.exit(1)
try:
import xlsxwriter
print("✅ XlsxWriter 库已安装") # 20260402 102700 增加xlsx导出依赖检查
except ImportError:
print("⚠️ 警告:XlsxWriter 库未安装,XLSX导出功能可能无法正常工作") # 20260402 102700 增加xlsx导出依赖检查
print(" 请运行: pip install XlsxWriter")
response = input("是否继续打包?(y/n): ")
if response.lower() != 'y':
sys.exit(1)
def build_exe():
print(f"=== DirTreeCopy {APP_VERSION} 打包脚本 ===")
print("检查依赖和文件...")
# 检查依赖库
check_dependencies()
# 检查必要文件是否存在
if not os.path.exists('main_app.py'):
print("❌ 错误:找不到 main_app.py 文件")
sys.exit(1)
else:
print("✅ main_app.py 文件存在")
if not os.path.exists('app_icon.ico'):
print("⚠️ 警告:找不到 app_icon.ico 文件,将使用默认图标")
icon_param = []
else:
print("✅ app_icon.ico 文件存在")
icon_param = ['--icon=app_icon.ico']
params = [
'main_app.py',
f'--name={BUILD_NAME}',
'--noconsole',
'--onefile',
'--clean',
'--windowed',
# 排除不需要的模块以减小体积
'--exclude-module=PIL',
'--exclude-module=matplotlib',
'--exclude-module=numpy',
'--exclude-module=pandas',
'--exclude-module=scipy',
'--exclude-module=IPython',
'--exclude-module=jupyter',
# 包含python-docx相关模块(确保DOCX功能正常)
'--hidden-import=docx',
'--hidden-import=docx.shared',
'--hidden-import=docx.enum.text',
'--hidden-import=docx.oxml.shared',
'--hidden-import=lxml', # python-docx的依赖
'--hidden-import=xlsxwriter', # 20260402 102700 确保XLSX导出依赖被打包进EXE
] + icon_param
print("\n=== 打包配置 ===")
print(f"目标文件: {BUILD_NAME}.exe")
print(f"打包模式: 单文件模式 (--onefile)")
print(f"窗口模式: 无控制台窗口 (--windowed)")
print(f"图标文件: {'已包含' if icon_param else '使用默认'}")
print(f"DOCX支持: 已包含 python-docx 相关模块")
try:
print("\n🚀 开始打包...")
PyInstaller.__main__.run(params)
# 检查生成的文件
exe_path = os.path.join('dist', f'{BUILD_NAME}.exe')
if os.path.exists(exe_path):
file_size = os.path.getsize(exe_path) / (1024 * 1024) # MB
print(f"\n✅ 打包完成!")
print(f"📁 输出文件: {exe_path}")
print(f"📊 文件大小: {file_size:.1f} MB")
print(f"\n🎉 DirTreeCopy {APP_VERSION} 已成功打包!")
print(f" 支持功能: 10种导出格式 + DOCX超链接") # 20260402 102700 更新导出格式数量说明
else:
print("⚠️ 打包完成,但未找到输出文件")
except Exception as e:
print(f"❌ 打包失败:{e}")
sys.exit(1)
if __name__ == "__main__":
build_exe()