本帖最后由 axelia 于 2024-11-21 14:39 编辑
看论坛里也有很多使用python写的,不过因为大部分场景都是win环境,还要装python环境和库,Windows下powershell都是自带的,使用比较方便,写了一个脚本,用来将文件夹下word、excel文件转pdf(excel多个sheet的时候,使用文件名_sheet分开转为pdf )
直接贴代码了,使用的时候修改下工作目录路径就行了
# 设置工作目录
$directory = "C:\\test2"
# 设置日志文件
$logFile = "C:\test2\conversion_log.txt"
# 函数:记录日志
function Log-Message {
param (
[string]$message
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $logFile -Value "$timestamp - $message"
}
# 检查文件是否存在
function Check-FileExists {
param (
[string]$filePath
)
if (-Not (Test-Path -Path $filePath)) {
Log-Message "File does not exist: $filePath"
return $false
}
return $true
}
# 清理 COM 对象
function Cleanup-ComObjects {
param (
[object[]]$comObjects
)
foreach ($comObject in $comObjects) {
if ($comObject -ne $null) {
try {
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($comObject) | Out-Null
}
catch {
Log-Message "Error releasing COM object: $_"
}
}
}
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
}
# 遍历目录中的所有文件
Get-ChildItem -Path $directory -Recurse | Where-Object { $_.PSIsContainer -eq $false } | ForEach-Object {
# 获取文件扩展名
$extension = $_.Extension.ToLower()
# 获取文件全路径
$filePath = $_.FullName
# 获取文件的基本名称和目录
$fileBaseName = $_.BaseName
$fileDirectory = $_.Directory.FullName
# 记录文件路径
Log-Message "Processing file: $filePath"
# 定义输出路径
$outputPath = Join-Path $_.directory ($_.BaseName + ".pdf")
# 加载Word和Excel COM对象
$wordApp = $null
$excelApp = $null
try {
$wordApp = New-Object -ComObject Word.Application
$excelApp = New-Object -ComObject Excel.Application
# 禁用 Word 提示
$wordApp.DisplayAlerts = $false
# 禁用 Excel 提示
$excelApp.DisplayAlerts = $false
# 根据文件类型进行处理
switch ($extension) {
'.doc' {
if (Check-FileExists -filePath $filePath) {
# 打开Word文档并保存为PDF
$document = $wordApp.Documents.Open($filePath)
$document.SaveAs($outputPath, 17) # 17是wdFormatPDF
$document.Close()
Log-Message "Converted $filePath to PDF successfully."
}
}
'.docx' {
if (Check-FileExists -filePath $filePath) {
# 同上
$document = $wordApp.Documents.Open($filePath)
$document.SaveAs($outputPath, 17)
$document.Close()
Log-Message "Converted $filePath to PDF successfully."
}
}
'.xls' {
if (Check-FileExists -filePath $filePath) {
# 打开Excel工作簿
$workbook = $excelApp.Workbooks.Open($filePath)
foreach ($worksheet in $workbook.Worksheets) {
$sheetName = $worksheet.Name
$outputPath = Join-Path $fileDirectory ($fileBaseName + "_" + $sheetName + ".pdf")
# 设置页面布局
$worksheet.PageSetup.Zoom = $false
$worksheet.PageSetup.FitToPagesWide = 1
$worksheet.PageSetup.FitToPagesTall = 1
$worksheet.PageSetup.PaperSize = 9
$worksheet.PageSetup.LeftMargin = $excelApp.InchesToPoints(0.75)
$worksheet.PageSetup.RightMargin = $excelApp.InchesToPoints(0.75)
$worksheet.PageSetup.TopMargin = $excelApp.InchesToPoints(1)
$worksheet.PageSetup.BottomMargin = $excelApp.InchesToPoints(1)
$worksheet.PageSetup.Orientation = 2
# 导出单个工作表为PDF
$worksheet.ExportAsFixedFormat(0, $outputPath) # 0是xlTypePDF
Log-Message "Converted sheet '$sheetName' from $filePath to PDF successfully."
}
$workbook.Close()
}
}
'.xlsx' {
if (Check-FileExists -filePath $filePath) {
# 打开Excel工作簿
$workbook = $excelApp.Workbooks.Open($filePath)
foreach ($worksheet in $workbook.Worksheets) {
$sheetName = $worksheet.Name
$outputPath = Join-Path $fileDirectory ($fileBaseName + "_" + $sheetName + ".pdf")
# 设置页面布局
$worksheet.PageSetup.Zoom = $false
$worksheet.PageSetup.FitToPagesWide = 1
$worksheet.PageSetup.FitToPagesTall = 1
$worksheet.PageSetup.PaperSize = 9
$worksheet.PageSetup.LeftMargin = $excelApp.InchesToPoints(0.75)
$worksheet.PageSetup.RightMargin = $excelApp.InchesToPoints(0.75)
$worksheet.PageSetup.TopMargin = $excelApp.InchesToPoints(1)
$worksheet.PageSetup.BottomMargin = $excelApp.InchesToPoints(1)
$worksheet.PageSetup.Orientation = 2
# 导出单个工作表为PDF
$worksheet.ExportAsFixedFormat(0, $outputPath) # 0是xlTypePDF
Log-Message "Converted sheet '$sheetName' from $filePath to PDF successfully."
}
$workbook.Close()
}
}
}
}
catch {
Log-Message "Error converting $filePath : $_"
}
finally {
# 关闭应用程序
if ($wordApp -ne $null) {
$wordApp.Quit()
}
if ($excelApp -ne $null) {
$excelApp.Quit()
}
# 清理 COM 对象
Cleanup-ComObjects @($wordApp, $excelApp)
}
}
|