setup_windows.ps1 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #Requires -Version 5.1
  2. <#
  3. .SYNOPSIS
  4. XLSX Skill — Environment Setup for Windows (Win10/Win11)
  5. .DESCRIPTION
  6. Detects platform, checks and installs all dependencies for the XLSX skill.
  7. Supports China mirror fallback for pip.
  8. #>
  9. param(
  10. [switch]$UseChinaMirror
  11. )
  12. $ErrorActionPreference = "Continue"
  13. function Write-Ok { param($msg) Write-Host " [OK] $msg" -ForegroundColor Green }
  14. function Write-Fail { param($msg) Write-Host " [FAIL] $msg" -ForegroundColor Red }
  15. function Write-Warn { param($msg) Write-Host " [WARN] $msg" -ForegroundColor Yellow }
  16. function Write-Info { param($msg) Write-Host " [->] $msg" -ForegroundColor Cyan }
  17. # ── Resolve XLSX_SKILL_DIR ──
  18. $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  19. $XLSX_SKILL_DIR = Split-Path -Parent $ScriptDir
  20. $env:XLSX_SKILL_DIR = $XLSX_SKILL_DIR
  21. Write-Host "============================================"
  22. Write-Host " XLSX Skill - Environment Setup"
  23. Write-Host " (Windows)"
  24. Write-Host "============================================"
  25. Write-Host ""
  26. # ── Step 1: Platform Detection ──
  27. $WinVer = [System.Environment]::OSVersion.Version
  28. $WinName = if ($WinVer.Build -ge 22000) { "Windows 11" } elseif ($WinVer.Build -ge 10240) { "Windows 10" } else { "Windows (older)" }
  29. Write-Host "Platform: $WinName (Build $($WinVer.Build)), $([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)"
  30. Write-Host "XLSX_SKILL_DIR=$XLSX_SKILL_DIR"
  31. Write-Host ""
  32. # ── China mirror detection ──
  33. $PipMirrorArgs = @()
  34. if ($UseChinaMirror) {
  35. $global:UseCN = $true
  36. } else {
  37. try {
  38. $null = Invoke-WebRequest -Uri "https://pypi.org" -TimeoutSec 3 -UseBasicParsing -ErrorAction Stop
  39. $global:UseCN = $false
  40. } catch {
  41. Write-Warn "pypi.org unreachable - enabling China mirrors"
  42. $global:UseCN = $true
  43. }
  44. }
  45. if ($global:UseCN) {
  46. $PipMirrorArgs = @("-i", "https://pypi.tuna.tsinghua.edu.cn/simple", "--trusted-host", "pypi.tuna.tsinghua.edu.cn")
  47. Write-Info "China mirrors enabled (pip: tuna)"
  48. Write-Host ""
  49. }
  50. $Errors = 0
  51. # ── Step 2a: Python 3 ──
  52. Write-Host "--- [1/5] Python 3 + pip ---"
  53. $PyCmd = $null
  54. foreach ($cmd in @("python3", "python", "py")) {
  55. try {
  56. $ver = & $cmd --version 2>&1
  57. if ($ver -match "Python 3") {
  58. $PyCmd = $cmd
  59. Write-Ok "$cmd ($ver)"
  60. break
  61. }
  62. } catch {}
  63. }
  64. if (-not $PyCmd) {
  65. Write-Fail "Python 3 not found"
  66. Write-Info "Install option 1: winget install Python.Python.3.11"
  67. Write-Info "Install option 2: https://www.python.org/downloads/"
  68. Write-Info "Install option 3: choco install python3"
  69. if ($global:UseCN) {
  70. Write-Info "China alt: https://npmmirror.com/mirrors/python/"
  71. }
  72. $Errors++
  73. }
  74. if ($PyCmd) {
  75. try {
  76. $pipVer = & $PyCmd -m pip --version 2>&1
  77. if ($pipVer -match "pip") {
  78. Write-Ok "pip ($pipVer)"
  79. } else { throw "no pip" }
  80. } catch {
  81. Write-Fail "pip not found"
  82. Write-Info "Install: $PyCmd -m ensurepip --upgrade"
  83. $Errors++
  84. }
  85. }
  86. Write-Host ""
  87. # ── Step 2b: Python packages ──
  88. Write-Host "--- [2/5] Python Packages (openpyxl, XlsxWriter) ---"
  89. $PyPkgs = @(
  90. @{ Module = "openpyxl"; Package = "openpyxl" },
  91. @{ Module = "xlsxwriter"; Package = "XlsxWriter" }
  92. )
  93. $MissingPy = @()
  94. if ($PyCmd) {
  95. foreach ($pkg in $PyPkgs) {
  96. try {
  97. $result = & $PyCmd -c "import $($pkg.Module); print(getattr($($pkg.Module), '__version__', 'ok'))" 2>&1
  98. if ($LASTEXITCODE -eq 0) {
  99. Write-Ok "$($pkg.Package) ($result)"
  100. } else { throw "not installed" }
  101. } catch {
  102. Write-Fail "$($pkg.Package) not installed"
  103. $MissingPy += $pkg.Package
  104. }
  105. }
  106. if ($MissingPy.Count -gt 0) {
  107. Write-Info "Installing: $($MissingPy -join ', ')"
  108. $installArgs = @("-m", "pip", "install") + $PipMirrorArgs + $MissingPy
  109. try {
  110. & $PyCmd @installArgs 2>&1 | Out-Null
  111. Write-Ok "Installed: $($MissingPy -join ', ')"
  112. } catch {
  113. Write-Fail "pip install failed. Try: $PyCmd -m pip install $($PipMirrorArgs -join ' ') $($MissingPy -join ' ')"
  114. $Errors++
  115. }
  116. }
  117. } else {
  118. Write-Fail "Python packages - skipped (Python not found)"
  119. }
  120. Write-Host ""
  121. # ── Step 2c: LibreOffice (install-on-demand, NOT substitutable) ──
  122. Write-Host "--- [3/5] LibreOffice (format conversion + formula recalc) ---"
  123. $SofficePath = "C:\Program Files\LibreOffice\program\soffice.exe"
  124. if (Test-Path $SofficePath) {
  125. Write-Ok "libreoffice found"
  126. } elseif (Get-Command soffice -ErrorAction SilentlyContinue) {
  127. Write-Ok "libreoffice (in PATH)"
  128. } else {
  129. Write-Warn "libreoffice not installed - REQUIRED for recalc, .xlsx -> PDF, .csv -> .xlsx"
  130. Write-Info "You MUST install it. FORBIDDEN: using Excel/WPS/Numbers instead of installing,"
  131. Write-Info "or skipping recalculation. A large download is NOT a reason to skip. REQUIRED:"
  132. Write-Info "explain the necessity to the user, say the package is large, THEN install it."
  133. Write-Info "PREFERRED source (fast in China, current build): Tsinghua TUNA mirror"
  134. Write-Info " https://mirrors.tuna.tsinghua.edu.cn/libreoffice/libreoffice/stable/26.8.0/"
  135. Write-Info " Windows x64: win/x86_64/LibreOffice_26.8.0_Win_x86-64.msi"
  136. Write-Info " Windows ARM64: win/aarch64/LibreOffice_26.8.0_Win_aarch64.msi"
  137. Write-Info " Then: msiexec /i <file>.msi /qn (needs admin)"
  138. Write-Info " If 26.8.0 is gone, pick the newest version under .../libreoffice/stable/"
  139. Write-Info "FALLBACK (only if the mirror is unreachable):"
  140. Write-Info " winget install TheDocumentFoundation.LibreOffice"
  141. Write-Info " or: https://www.libreoffice.org/download/"
  142. Write-Info "AFTER installing, REGISTER it on PATH (Windows method - differs per OS):"
  143. Write-Info ' setx PATH "$([Environment]::GetEnvironmentVariable(''PATH'',''User''));C:\Program Files\LibreOffice\program"'
  144. Write-Info " setx affects NEW shells only - for this session use the full soffice.exe path."
  145. Write-Info ' then re-verify: & "C:\Program Files\LibreOffice\program\soffice.exe" --version'
  146. Write-Info "An install left off PATH looks 'not installed' next run and gets reinstalled"
  147. Write-Info "needlessly. If soffice already exists on disk but is off PATH, just register it."
  148. Write-Info "Another program is allowed ONLY after an install attempt has FAILED. Report it."
  149. Write-Info "No install attempt = no substitute program."
  150. }
  151. Write-Host ""
  152. # ── Step 2d: Font Installation (from CDN) ──
  153. Write-Host "--- [4/5] Font Installation ---"
  154. $FontCdnBase = "https://z-cdn.chatglm.cn/office-skill/fonts"
  155. $FontList = Join-Path $ScriptDir "font_list.txt"
  156. $UserFontDir = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\Fonts"
  157. if (-not (Test-Path $UserFontDir)) { New-Item -ItemType Directory -Path $UserFontDir -Force | Out-Null }
  158. $Marker = Join-Path $UserFontDir ".office-skill-fonts-installed"
  159. if (Test-Path $Marker) {
  160. Write-Ok "Fonts already installed (marker found). To re-install, delete $Marker"
  161. } else {
  162. if (-not (Test-Path $FontList)) {
  163. Write-Fail "Font list not found: $FontList"
  164. $Errors++
  165. } else {
  166. $lines = Get-Content $FontList | Where-Object { $_.Trim() -ne "" }
  167. $Total = $lines.Count
  168. $Installed = 0; $Skipped = 0; $Failed = 0
  169. Write-Info "Downloading $Total fonts from CDN..."
  170. foreach ($relPath in $lines) {
  171. $fname = Split-Path $relPath -Leaf
  172. $dest = Join-Path $UserFontDir $fname
  173. if (Test-Path $dest) {
  174. $Skipped++
  175. continue
  176. }
  177. $encoded = $relPath -replace '\[','%5B' -replace '\]','%5D' -replace ' ','%20'
  178. $url = "$FontCdnBase/$encoded"
  179. try {
  180. Invoke-WebRequest -Uri $url -OutFile $dest -TimeoutSec 30 -ErrorAction Stop
  181. $regPath = "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
  182. $null = New-ItemProperty -Path $regPath -Name $fname -Value $dest -PropertyType String -Force -ErrorAction SilentlyContinue
  183. $Installed++
  184. } catch {
  185. Write-Warn "Failed to download: $relPath"
  186. $Failed++
  187. Remove-Item $dest -Force -ErrorAction SilentlyContinue
  188. }
  189. }
  190. if ($Failed -eq 0) {
  191. New-Item -ItemType File -Path $Marker -Force | Out-Null
  192. Write-Ok "Fonts: $Installed newly installed, $Skipped already present (target: $UserFontDir)"
  193. } else {
  194. Write-Warn "Fonts: $Installed installed, $Skipped skipped, $Failed failed (marker not written, will retry next run)"
  195. $Errors++
  196. }
  197. }
  198. }
  199. # Set XLSX_FONTS_DIR to user font directory (fonts are now installed there)
  200. $XLSX_FONTS_DIR = $UserFontDir
  201. $env:XLSX_FONTS_DIR = $XLSX_FONTS_DIR
  202. Write-Host ""
  203. # ── Step 2e: CJK Font Verification ──
  204. Write-Host "--- [5/5] CJK Font Verification ---"
  205. $CjkFound = $false
  206. $FontsDir = Join-Path $env:WINDIR "Fonts"
  207. $UserFontDir2 = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\Fonts"
  208. $CjkFonts = @("NotoSansSC[wght].ttf", "NotoSerifSC-Regular.ttf")
  209. foreach ($f in $CjkFonts) {
  210. if ((Test-Path (Join-Path $FontsDir $f)) -or (Test-Path (Join-Path $UserFontDir2 $f))) {
  211. Write-Ok "CJK font found: $f"
  212. $CjkFound = $true
  213. break
  214. }
  215. }
  216. if (-not $CjkFound) {
  217. Write-Warn "No CJK font verified yet - fonts were just installed, restart may be needed"
  218. }
  219. Write-Host ""
  220. # ── Summary ──
  221. Write-Host "============================================"
  222. if ($Errors -eq 0) {
  223. Write-Host " All dependencies OK."
  224. } else {
  225. Write-Host " $Errors issue(s) found. Fix them above."
  226. }
  227. Write-Host " XLSX_SKILL_DIR=$XLSX_SKILL_DIR"
  228. Write-Host " XLSX_FONTS_DIR=$XLSX_FONTS_DIR (user font directory)"
  229. Write-Host "============================================"