File size: 1,099 Bytes
afe6e34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 將 題庫 資料夾中的 .doc/.docx 轉成「篩選過的 HTML」(保留圖片),輸出到 tools/_export
# 用法:powershell -ExecutionPolicy Bypass -File tools\export_docs.ps1
$root = Split-Path -Parent $PSScriptRoot
# 避免中文路徑字面值的編碼問題:抓 root 底下第一個「非 tools/data/js/css」的資料夾當題庫來源
$src = Get-ChildItem $root -Directory | Where-Object { @('tools','data','js','css','.git','node_modules','.agents') -notcontains $_.Name } | Select-Object -First 1
$out = Join-Path $PSScriptRoot "_export"
New-Item -ItemType Directory -Force -Path $out | Out-Null

$word = New-Object -ComObject Word.Application
$word.Visible = $false
$word.DisplayAlerts = 0
try {
  Get-ChildItem $src.FullName -File | Where-Object { $_.Extension -match '^\.docx?$' } | ForEach-Object {
    $doc = $word.Documents.Open($_.FullName, $false, $true)
    $target = Join-Path $out ($_.BaseName + ".htm")
    # 10 = wdFormatFilteredHTML
    $doc.SaveAs2([string]$target, 10)
    $doc.Close([ref]$false)
    Write-Output ("OK " + $_.Name)
  }
} finally {
  $word.Quit()
}