我有數以千計的 PDF 檔案,我正在嘗試梳理這些檔案并僅提取某些資料。我已經成功創建了一個腳本,它遍歷每個 PDF,將其內容放入 .txt,然后在最終的 .txt 中搜索所需的資訊。我唯一堅持的部分是嘗試將每個 PDF 中的所有資料合并到這個 .txt 檔案中。目前,每個連續的 PDF 只是簡單地覆寫以前的資料,并且僅在檔案夾中的最終 PDF 上執行搜索。如何更改這組代碼以允許將每一位資訊連接到 .txt 而不是覆寫?
$all = Get-Childitem -Path $file1 -Recurse -Filter *.pdf
foreach ($f in $all){
$outfile = -join ', '
$text = convert-PDFtoText $outfile
}
這是我的整個腳本以供參考:
Start-Process powershell.exe -Verb RunAs {
function convert-PDFtoText {
param(
[Parameter(Mandatory=$true)][string]$file
)
Add-Type -Path "C:\ps\itextsharp.dll"
$pdf = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $file
for ($page = 1; $page -le $pdf.NumberOfPages; $page ){
$text=[iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($pdf,$page)
Write-Output $text
}
$pdf.Close()
}
$content = Read-Host "What are we looking for?: "
$file1 = Read-Host "Path to search: "
$all = Get-Childitem -Path $file1 -Recurse -Filter *.pdf
foreach ($f in $all){
$outfile = $f -join ', '
$text = convert-PDFtoText $outfile
}
$text | Out-File "C:\ps\bulk.txt"
Select-String -Path C:\ps\bulk.txt -Pattern $content | Out-File "C:\ps\select.txt"
Start-Sleep -Seconds 60
}
任何幫助將不勝感激!
uj5u.com熱心網友回復:
要convert-PDFtoText在單個輸出檔案中捕獲所有輸出,請使用帶有ForEach-Objectcmdlet的單個管道:
Get-ChildItem -Path $file1 -Recurse -Filter *.pdf |
ForEach-Object { convert-PDFtoText $_.FullName } |
Out-File "C:\ps\bulk.txt"
對您的convert-PDFtoText功能進行調整將允許更簡潔和有效的解決方案:
請convert-PDFtoText接受Get-ChildItem直接從管道輸入:
function convert-PDFtoText {
param(
[Alias('FullName')
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string] $file
)
begin {
Add-Type -Path "C:\ps\itextsharp.dll"
}
process {
$pdf = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $file
for ($page = 1; $page -le $pdf.NumberOfPages; $page ) {
[iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($pdf,$page)
}
$pdf.Close()
}
}
然后,這允許您將頂部的命令簡化為:
Get-ChildItem -Path $file1 -Recurse -Filter *.pdf |
convert-PDFtoText |
Out-File "C:\ps\bulk.txt"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/345217.html
標籤:电源外壳
