TableSynthesis/build_windows.ps1
2025-07-02 19:43:52 +08:00

226 lines
7.8 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 座位分配系统 Windows 打包脚本 (PowerShell版本)
# 编码UTF-8
Write-Host "================================" -ForegroundColor Cyan
Write-Host " 座位分配系统 Windows 打包脚本" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan
Write-Host ""
# 设置错误处理
$ErrorActionPreference = "Stop"
try {
# 检查Python是否安装
Write-Host "[信息] 检查Python环境..." -ForegroundColor Yellow
$pythonVersion = python --version 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Python未安装或不在PATH中请先安装Python 3.7+"
}
Write-Host "Python版本: $pythonVersion" -ForegroundColor Green
# 检查pip
$pipVersion = pip --version 2>&1
if ($LASTEXITCODE -ne 0) {
throw "pip不可用"
}
Write-Host "pip版本: $pipVersion" -ForegroundColor Green
# 升级pip
Write-Host ""
Write-Host "[步骤1] 升级pip..." -ForegroundColor Yellow
python -m pip install --upgrade pip
# 安装PyInstaller
Write-Host ""
Write-Host "[步骤2] 安装PyInstaller..." -ForegroundColor Yellow
pip install pyinstaller
# 安装依赖包
Write-Host ""
Write-Host "[步骤3] 安装依赖包..." -ForegroundColor Yellow
pip install pandas numpy openpyxl
# 创建必要的目录
Write-Host ""
Write-Host "[步骤4] 创建构建目录..." -ForegroundColor Yellow
$dirs = @("build", "dist", "release", "release\示例文件")
foreach ($dir in $dirs) {
if (!(Test-Path $dir)) {
New-Item -ItemType Directory -Path $dir -Force | Out-Null
Write-Host "创建目录: $dir" -ForegroundColor Gray
}
}
# 检查主文件是否存在
if (!(Test-Path "seat_allocation_system.py")) {
throw "找不到主程序文件 seat_allocation_system.py"
}
# 打包主程序
Write-Host ""
Write-Host "[步骤5] 开始打包座位分配系统..." -ForegroundColor Yellow
Write-Host "这可能需要几分钟时间,请耐心等待..." -ForegroundColor Gray
$pyinstallerArgs = @(
"--onefile",
"--console",
"--name", "座位分配系统",
"--distpath", "dist",
"--workpath", "build",
"--specpath", "build",
"--clean",
"seat_allocation_system.py"
)
# 如果存在图标文件,添加图标参数
if (Test-Path "icon.ico") {
$pyinstallerArgs += "--icon", "icon.ico"
}
# 如果存在README文件添加数据文件
if (Test-Path "README.md") {
$pyinstallerArgs += "--add-data", "README.md;."
}
& pyinstaller @pyinstallerArgs
if ($LASTEXITCODE -ne 0) {
throw "PyInstaller打包失败"
}
# 检查打包结果
$exePath = "dist\座位分配系统.exe"
if (!(Test-Path $exePath)) {
throw "打包失败:找不到生成的可执行文件"
}
# 复制文件到发布目录
Write-Host ""
Write-Host "[步骤6] 创建发布包..." -ForegroundColor Yellow
# 复制主程序
Copy-Item $exePath "release\" -Force
Write-Host "复制主程序到发布目录" -ForegroundColor Gray
# 复制示例文件
$exampleFiles = @("人员信息_示例.xlsx", "座位信息_示例.xlsx", "*.xlsx")
foreach ($pattern in $exampleFiles) {
$files = Get-ChildItem -Path . -Name $pattern -ErrorAction SilentlyContinue
foreach ($file in $files) {
if ($file -notlike "*_最终分配*" -and $file -notlike "*日志*") {
Copy-Item $file "release\示例文件\" -Force -ErrorAction SilentlyContinue
Write-Host "复制示例文件: $file" -ForegroundColor Gray
}
}
}
# 创建使用说明
Write-Host ""
Write-Host "[步骤7] 创建使用说明..." -ForegroundColor Yellow
$readme = @"
v2.0
==================
使
1. Excel
2. ".exe"
3.
-
-
- Excel
- 1-10
-
-
-
-
log
- log/_.xlsx
- log/.xlsx
- log/seat_allocation_log.txt
1.
2.
3.
4.
5.
- Excel
- ""
-
-
log
- v2.0 ()
- Windows 7/8/10/11
- Python
"@
$readme | Out-File -FilePath "release\使用说明.txt" -Encoding UTF8
# 创建批处理启动文件(可选)
$batchContent = @"
@echo off
chcp 65001 >nul
echo ...
echo.
.exe
echo.
echo
pause
"@
$batchContent | Out-File -FilePath "release\启动座位分配系统.bat" -Encoding UTF8
# 显示完成信息
Write-Host ""
Write-Host "================================" -ForegroundColor Green
Write-Host " 打包完成!" -ForegroundColor Green
Write-Host "================================" -ForegroundColor Green
Write-Host ""
Write-Host "发布文件位置:" -ForegroundColor Cyan
Write-Host "📁 release\" -ForegroundColor Yellow
Write-Host " ├── 座位分配系统.exe (主程序)" -ForegroundColor White
Write-Host " ├── 启动座位分配系统.bat (启动脚本)" -ForegroundColor White
Write-Host " ├── 使用说明.txt (使用说明)" -ForegroundColor White
Write-Host " └── 示例文件\ (示例文件夹)" -ForegroundColor White
Write-Host ""
Write-Host "文件大小信息:" -ForegroundColor Cyan
$fileSize = (Get-Item $exePath).Length / 1MB
Write-Host "主程序大小: $([math]::Round($fileSize, 2)) MB" -ForegroundColor Yellow
Write-Host ""
Write-Host "部署说明:" -ForegroundColor Cyan
Write-Host "1. 将整个 release 文件夹分发给用户" -ForegroundColor White
Write-Host "2. 用户直接运行 座位分配系统.exe 或 启动座位分配系统.bat" -ForegroundColor White
Write-Host "3. 无需安装Python环境" -ForegroundColor White
Write-Host ""
Write-Host "✅ 打包成功完成!" -ForegroundColor Green
} catch {
Write-Host ""
Write-Host "❌ 打包过程中出现错误:" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
Write-Host ""
Write-Host "常见解决方案:" -ForegroundColor Yellow
Write-Host "1. 确保已安装Python 3.7+" -ForegroundColor White
Write-Host "2. 确保网络连接正常(需要下载依赖包)" -ForegroundColor White
Write-Host "3. 确保有足够的磁盘空间" -ForegroundColor White
Write-Host "4. 尝试以管理员权限运行此脚本" -ForegroundColor White
exit 1
}
Write-Host ""
Write-Host "按任意键继续..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")