doc_to_docx.ps1 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # doc_to_docx.ps1 — Windows mirror of doc_to_docx.sh
  2. # Convert .doc to .docx using LibreOffice (soffice.exe).
  3. #Requires -Version 5.1
  4. [CmdletBinding()]
  5. param(
  6. [Parameter(Mandatory, Position = 0)]
  7. [string]$InputPath,
  8. [Parameter(Position = 1)]
  9. [string]$OutDir = '.'
  10. )
  11. $ErrorActionPreference = 'Stop'
  12. # soffice.exe handles its own internal Unicode but emits stderr in the active console code page;
  13. # we want UTF-8 so error messages render correctly on CN/JP/KR Windows.
  14. try { & chcp.com 65001 *> $null } catch { }
  15. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  16. [Console]::InputEncoding = [System.Text.Encoding]::UTF8
  17. $OutputEncoding = [System.Text.UTF8Encoding]::new()
  18. function Resolve-SofficePath {
  19. foreach ($n in @('soffice.exe', 'soffice')) {
  20. $cmd = Get-Command $n -ErrorAction SilentlyContinue
  21. if ($cmd) { return $cmd.Source }
  22. }
  23. $candidates = @(
  24. (Join-Path $env:ProgramFiles 'LibreOffice\program\soffice.exe'),
  25. (Join-Path ${env:ProgramFiles(x86)} 'LibreOffice\program\soffice.exe'),
  26. (Join-Path $env:LOCALAPPDATA 'Programs\LibreOffice\program\soffice.exe'),
  27. (Join-Path $env:LOCALAPPDATA 'Microsoft\WinGet\Links\soffice.exe'),
  28. (Join-Path $env:USERPROFILE 'scoop\apps\libreoffice\current\program\soffice.exe')
  29. ) | Where-Object { $_ }
  30. foreach ($p in $candidates) { if (Test-Path $p) { return (Resolve-Path $p).Path } }
  31. return $null
  32. }
  33. if (-not (Test-Path -LiteralPath $InputPath -PathType Leaf)) {
  34. Write-Host "Error: File not found: $InputPath" -ForegroundColor Red
  35. exit 1
  36. }
  37. $soffice = Resolve-SofficePath
  38. if (-not $soffice) {
  39. Write-Host "Error: soffice.exe (LibreOffice) is required for .doc conversion but not found." -ForegroundColor Red
  40. Write-Host "Install: winget install TheDocumentFoundation.LibreOffice"
  41. Write-Host "Or: choco install libreoffice-fresh -y"
  42. Write-Host "Or download: https://www.libreoffice.org/download/"
  43. exit 1
  44. }
  45. $resolvedIn = (Resolve-Path -LiteralPath $InputPath).Path
  46. $baseName = [IO.Path]::GetFileNameWithoutExtension($resolvedIn)
  47. if (-not (Test-Path -LiteralPath $OutDir)) {
  48. New-Item -ItemType Directory -Path $OutDir -Force | Out-Null
  49. }
  50. $resolvedOut = (Resolve-Path -LiteralPath $OutDir).Path
  51. $outputFile = Join-Path $resolvedOut "$baseName.docx"
  52. Write-Host "Converting: $resolvedIn -> $outputFile"
  53. # soffice user profile must be unique per invocation to avoid the "another instance is already running" lock.
  54. $profileDir = Join-Path $env:TEMP ("docx-soffice-profile-" + [System.IO.Path]::GetRandomFileName())
  55. $profileUri = 'file:///' + ($profileDir -replace '\\', '/')
  56. try {
  57. & $soffice `
  58. --headless `
  59. --norestore `
  60. --nolockcheck `
  61. "-env:UserInstallation=$profileUri" `
  62. --convert-to docx `
  63. --outdir $resolvedOut `
  64. $resolvedIn *> $null
  65. $code = $LASTEXITCODE
  66. if (-not (Test-Path -LiteralPath $outputFile)) {
  67. Write-Host "Error: Conversion failed. Output file not created: $outputFile (soffice exit=$code)" -ForegroundColor Red
  68. exit 1
  69. }
  70. Write-Host "Success: $outputFile"
  71. } finally {
  72. Remove-Item -LiteralPath $profileDir -Recurse -Force -ErrorAction SilentlyContinue
  73. }