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

时间:2018-08-13 04:39:23

标签: powershell ms-word export-to-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:\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 Versions:    
(a) SQL Server 2016 to be used for all upcoming projects;
(b) SQL Server 2016 Enterprise edition will be used on all servers
(c) Assumptions:
    (i) SQL Server will be installed on VM instances

我正在尝试的代码:

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()

1 个答案:

答案 0 :(得分:0)

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

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