使用Powershell将PDF导入Word

问题描述 投票:0回答:1

我正在运行以下代码将大型 PDF 文件转换为 Word 并通过 Powershell 执行它们,但仍然遇到以下错误。我已经将confirmConversions/link/attachment参数显式地转换为一个对象,但这没有帮助。

# Define paths
$folderPath = "my source folder"
$resultPath = "my destination folder"

# Create Word application COM object
$wordApp = New-Object -ComObject Word.Application
$wordApp.Visible = $false  # Set Word to run in the background

# Create FileSystemObject COM object
$fso = New-Object -ComObject Scripting.FileSystemObject

# Get the folder
$folder = $fso.GetFolder($folderPath)

# Loop through each file in the folder
foreach ($file in $folder.Files) {
    # Create a new Word document
    $newDoc = $wordApp.Documents.Add()

    # Explicitly cast the boolean and object parameters as [ref] objects for the InsertFile method
    $confirmConversions = [ref] $false
    $link = [ref] $false
    $attachment = [ref] $false

    # Insert the content of the current file into the new document
    $newDoc.Range().InsertFile($file.Path, [ref] $null, $confirmConversions, $link, $attachment)

    # Optionally, add some additional text or actions to the new document
    $newDoc.Content.InsertBefore("Processed document: " + $file.Name + "`n")

    # Define the document name and save it
    $docName = [System.IO.Path]::Combine($resultPath, ($file.Name -replace ".\w+$", "_processed.docx"))
    $newDoc.SaveAs([ref] $docName, [ref] 16)  # 16 = wdFormatXMLDocument for .docx

    # Close the document after saving
    $newDoc.Close([ref] $false)
}

# Cleanup
$wordApp.Quit()
$fso = $null
[System.GC]::Collect()  # Force garbage collection to release COM objects

错误:

Exception setting "InsertFile": Cannot convert the "False" value of type "bool" to type "Object".
At [my source folder]#ProcessWordDocs.ps1:26 char:5
+     $newDoc.Range().InsertFile($file.Path, [ref] $null, $confirmConve ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : RuntimeException

关于如何解决这个问题有什么想法吗?

powershell pdf
1个回答
0
投票

这是一种不使用

InsertFile()
方法即可完成此操作的方法。
您只需用word打开pdf即可将其转换为文档,因此之后您需要做的就是将文档保存到具有新扩展名的不同路径:

# Define paths
$folderPath = "my source folder"
$resultPath = "my destination folder"  # make sure the path exists

# Create Word application COM object
$wordApp = New-Object -ComObject Word.Application
$wordApp.Visible = $false  # Set Word to run in the background

# Loop through each file in the folder
foreach ($file in (Get-ChildItem -Path $folderPath -Filter '*.pdf' -File)) {
    # Create a new Word document
    $newDoc = $wordApp.Documents.Open($file.FullName) #, $false, $false, $false)

    # Optionally, add some additional text or actions to the new document
    $newDoc.Content.InsertBefore("Processed document: " + $file.Name + "`r`n")

    # Combine the document new path and name and save it
    $docName = Join-Path -Path $resultPath -ChildPath ('{0}_processed.docx' -f $file.BaseName)
    $wordApp.ActiveDocument.SaveAs("$docName", 16)   # 16 = wdFormatXMLDocument for .docx
    # Close the document after saving
    $newDoc.Close()
}

# quit Word and cleanup the used COM objects
$wordApp.Quit()

$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($newDoc)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wordApp)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
© www.soinside.com 2019 - 2024. All rights reserved.