-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_exe.py
More file actions
301 lines (266 loc) · 8.34 KB
/
Copy pathbuild_exe.py
File metadata and controls
301 lines (266 loc) · 8.34 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
import sys
import subprocess
import shutil
from pathlib import Path
def install_requirements():
"""安装必需的依赖"""
print("正在安装必需的依赖...")
requirements = [
"pyinstaller",
"markitdown[all]", # 完整版markitdown包
]
# 安装所有依赖
for req in requirements:
print(f"安装 {req}...")
subprocess.run([sys.executable, "-m", "pip", "install", req], check=True)
def create_spec_file():
"""创建PyInstaller规范文件"""
spec_content = '''# -*- mode: python ; coding: utf-8 -*-
import os
import sys
from pathlib import Path
import site
# 获取magika模型文件路径
try:
import magika
magika_package_path = os.path.dirname(magika.__file__)
magika_models = os.path.join(magika_package_path, 'models')
magika_config = os.path.join(magika_package_path, 'config')
except ImportError:
magika_models = None
magika_config = None
# 准备magika数据文件
magika_datas = []
if magika_models and os.path.exists(magika_models):
magika_datas.append((magika_models, 'magika/models'))
if magika_config and os.path.exists(magika_config):
magika_datas.append((magika_config, 'magika/config'))
block_cipher = None
a = Analysis(
['markitdown_gui.py'],
pathex=['.'],
binaries=[],
datas=magika_datas,
hiddenimports=[
'markitdown',
'markitdown._markitdown',
'markitdown.converters',
'markitdown.converters._pdf_converter',
'markitdown.converters._docx_converter',
'markitdown.converters._xlsx_converter',
'markitdown.converters._pptx_converter',
'markitdown.converters._html_converter',
'markitdown.converters._image_converter',
'markitdown.converters._audio_converter',
'markitdown.converters._plain_text_converter',
'markitdown.converters._csv_converter',
'markitdown.converters._epub_converter',
'markitdown.converters._zip_converter',
'markitdown.converters._ipynb_converter',
'markitdown.converters._rss_converter',
'markitdown.converters._wikipedia_converter',
'markitdown.converters._youtube_converter',
'markitdown.converters._bing_serp_converter',
'markitdown.converters._outlook_msg_converter',
'markitdown.converters._doc_intel_converter',
'markitdown.converter_utils',
'markitdown.converter_utils.docx',
'markitdown.converter_utils.docx.math',
'tkinter',
'tkinter.ttk',
'tkinter.filedialog',
'tkinter.messagebox',
'tkinter.scrolledtext',
'threading',
'requests',
'magika',
'magika.magika',
'magika.types',
'magika.config',
'onnxruntime',
'onnxruntime.capi.onnxruntime_pybind11_state',
'numpy',
'charset_normalizer',
'codecs',
'mimetypes',
'xml.etree.ElementTree',
'zipfile',
'json',
'csv',
'base64',
'urllib.parse',
'urllib.request',
'html.parser',
'bs4',
'pandas',
'openpyxl',
'xlrd',
'python-pptx',
'pypdf',
'PIL',
'eyed3',
'pydub',
'ebooklib',
'python-docx',
'mammoth',
'lxml',
'beautifulsoup4',
'feedparser',
'youtube-dl',
'yt-dlp',
'extract-msg',
],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='MarkItDown-GUI',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
version='version_info.txt',
icon='icon.ico' if os.path.exists('icon.ico') else None,
)
'''
with open('markitdown_gui.spec', 'w', encoding='utf-8') as f:
f.write(spec_content)
print("已创建PyInstaller规范文件: markitdown_gui.spec")
def create_version_info():
"""创建版本信息文件"""
version_info = '''# UTF-8
#
# For more details about fixed file info 'ffi' see:
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
VSVersionInfo(
ffi=FixedFileInfo(
filevers=(1,0,0,0),
prodvers=(1,0,0,0),
mask=0x3f,
flags=0x0,
OS=0x40004,
fileType=0x1,
subtype=0x0,
date=(0, 0)
),
kids=[
StringFileInfo(
[
StringTable(
u'040904B0',
[StringStruct(u'CompanyName', u'MarkItDown GUI'),
StringStruct(u'FileDescription', u'MarkItDown文件转换工具'),
StringStruct(u'FileVersion', u'1.0.0.0'),
StringStruct(u'InternalName', u'MarkItDown-GUI'),
StringStruct(u'LegalCopyright', u'Based on Microsoft MarkItDown'),
StringStruct(u'OriginalFilename', u'MarkItDown-GUI.exe'),
StringStruct(u'ProductName', u'MarkItDown GUI'),
StringStruct(u'ProductVersion', u'1.0.0.0')])
]),
VarFileInfo([VarStruct(u'Translation', [1033, 1200])])
]
)
'''
with open('version_info.txt', 'w', encoding='utf-8') as f:
f.write(version_info)
print("已创建版本信息文件: version_info.txt")
def create_icon():
"""创建或复制图标文件"""
# 如果没有图标文件,创建一个简单的说明
if not os.path.exists('icon.ico'):
print("提示: 如果您有图标文件(.ico格式),请将其命名为'icon.ico'并放在当前目录中")
print(" 这样生成的exe文件就会有自定义图标")
def build_exe():
"""构建exe文件"""
print("开始构建exe文件...")
# 使用PyInstaller构建
cmd = [
sys.executable, "-m", "PyInstaller",
"markitdown_gui.spec",
"--clean",
"--noconfirm"
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
print("构建成功!")
print("exe文件位置: dist/MarkItDown-GUI.exe")
# 检查文件大小
exe_path = Path("dist/MarkItDown-GUI.exe")
if exe_path.exists():
size_mb = exe_path.stat().st_size / (1024 * 1024)
print(f"文件大小: {size_mb:.1f} MB")
return True
else:
print("构建失败!")
print("错误输出:")
print(result.stderr)
return False
def cleanup():
"""清理临时文件"""
print("清理临时文件...")
dirs_to_remove = ['build', '__pycache__']
files_to_remove = ['markitdown_gui.spec', 'version_info.txt']
for dir_name in dirs_to_remove:
if os.path.exists(dir_name):
shutil.rmtree(dir_name)
print(f"已删除目录: {dir_name}")
for file_name in files_to_remove:
if os.path.exists(file_name):
os.remove(file_name)
print(f"已删除文件: {file_name}")
def main():
"""主函数"""
print("=" * 50)
print("MarkItDown GUI - exe构建脚本")
print("=" * 50)
try:
# 步骤1: 安装依赖
print("\n步骤1: 安装依赖")
install_requirements()
# 步骤2: 创建配置文件
print("\n步骤2: 创建配置文件")
create_spec_file()
create_version_info()
create_icon()
# 步骤3: 构建exe
print("\n步骤3: 构建exe文件")
success = build_exe()
if success:
print("\n" + "=" * 50)
print("构建完成!")
print("exe文件位置: dist/MarkItDown-GUI.exe")
print("您可以运行该exe文件来使用MarkItDown GUI")
print("=" * 50)
# 询问是否清理临时文件
response = input("\n是否清理临时文件?(y/n): ").lower().strip()
if response in ['y', 'yes', '是']:
cleanup()
else:
print("\n构建失败,请检查错误信息")
except Exception as e:
print(f"\n构建过程中发生错误: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()