将Word文档中提取的内容另存为CSV文件



成功从Word文档中提取内容,但是我们如何将其保存为CSV文件 - 提取的所有数据都在CSV中的一行中。

Clear-Host
function ExtractSectionsFromWordDoc{
Param(
[string]$SourceFile,
[string]$SearchKeyword1,
[string]$SearchKeyword2
)
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$doc = $word.Documents.Open($SourceFile, $false, $true)
$sel = $word.Selection 
$paras = $doc.Paragraphs 
foreach ($para in $paras) { 
if ($para.Range.Text -match $SearchKeyword1) {
$startPosition = $para.Range.Start
}
if ($para.Range.Text -match $SearchKeyword2) {
$endPosition = $para.Range.Start
break
}
} 
$doc.Range($startPosition, $endPosition).Copy() 
$newdoc = $word.Documents.Add()
$newdoc.Content.Paste()
$newdoc.SaveAs("D:testingSearch1.doc")
$newdoc.Close()
# cleanup com objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}

文档内容是这样的,我们想在 sql 服务器中保存为一行 关于文档的说明:

SQL Server 版本:    (a( SQL Server 2016将用于所有即将开展的项目; (b( SQL Server 2016 Enterprise 版将在所有服务器上使用 (c( 假设: (i( SQL Server 将安装在 VM 实例上

我正在尝试的代码:

clear-host
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$Document=$Word.documents.open("D:testingSearch1.doc", $false, $true)
$range = $Document.content
[array]$content = $range.Text 
$Output = $content | Out-String
$Output | Out-File d:Testingtemptxt.txt
Import-CSV d:Testingtemptxt.txt -Delimiter “|”| Export-CSV "D:testingtemp.csv" -NoTypeInformation
#Get-Content $content | Export-Csv -Path "D:TestingExcelfile.csv" 
# cleanup com objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Document) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

您需要使用保存文本的属性创建自定义对象,然后将这些对象导出为 CSV:

New-Object -Type PSObject -Property @{
'foo' = $document.Content.Range.Text | Out-String
} | Export-Csv 'output.csv' -NoType

最新更新