一、Powershell批量将doc转为docx
前提是已经安装office
# 创建 Word 应用对象
$word = New-Object -ComObject Word.Application
$word.Visible = $false
# 遍历指定目录及子目录中的所有 .doc 文件
# Get-ChildItem -Path "C:\path\to\docs" -Recurse -Include *.doc | ForEach-Object {
# 我手动放到一个目录下,没有子目录
Get-ChildItem -Filter *.doc | ForEach-Object {
$doc = $word.Documents.Open($_.FullName)
$newPath = $_.FullName -replace "\.doc$", ".docx"
# 12 表示保存为 DOCX 格式
$doc.SaveAs([ref] $newPath, [ref] 12)
$doc.Close()
}
# 退出 Word 应用
$word.Quit()
二、Powershell批量将docx转markdown
前提是已经安装pandoc
# 创建输出目录
New-Item -ItemType Directory -Force -Path output
#New-Item -ItemType Directory -Force -Path output/images
# 遍历所有 DOCX 文件
Get-ChildItem -Filter *.docx | ForEach-Object {
$filename = $_.BaseName
pandoc -s $_.FullName -t markdown -o "output/$filename.md"
Write-Host "已转换: $($_.Name) -> $filename.md"
}
Write-Host "批量转换完成!"