成功从 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:\testing\Search1.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 server中保存为一行 关于文档的描述:
SQL Server 版本: (a) SQL Server 2016 用于所有即将开展的项目; (b) SQL Server 2016 企业版将用于所有服务器 (c) 假设: (i) SQL Server 将安装在 VM 实例上
我正在尝试的代码:
clear-host
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$Document=$Word.documents.open("D:\testing\Search1.doc", $false, $true)
$range = $Document.content
[array]$content = $range.Text
$Output = $content | Out-String
$Output | Out-File d:\Testing\temptxt.txt
Import-CSV d:\Testing\temptxt.txt -Delimiter “|”| Export-CSV "D:\testing\temp.csv" -NoTypeInformation
#Get-Content $content | Export-Csv -Path "D:\Testing\Excelfile.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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句