EvotecIT / PSWriteOffice

Experimental PowerShell Module to create and edit Microsoft Word, Microsoft Excel, and Microsoft PowerPoint documents without having Microsoft Office installed.
107 stars 8 forks source link

Search and Replace Text #1

Open jmeldrum76 opened 2 years ago

jmeldrum76 commented 2 years ago

I would love to see the project to be able to search and replace text/hyperlinks in the a word document. This would be incredibly helpful.

PrzemyslawKlys commented 2 years ago

If you're not afraid of doing things manually a bit it's already possible to just go thru all hyperlinks and find what you're looking for and just replace it by setting Uri to new URL.

# This is just a show what can be quickly done using .NET before I get to do it's PowerShell version

Clear-Host
Import-Module .\PSWriteOffice.psd1 -Force

$Document = New-OfficeWord -FilePath $PSScriptRoot\Documents\BasicDocumentWithHyperlinks.docx
$Document.BuiltinDocumentProperties.Title = "This is title"
$Document.BuiltinDocumentProperties.Subject = "This is subject aka subtitle"

$null = $Document.AddHyperLink("Google", [uri] "https://www.google.com")

$Null = $Document.AddHyperLink("Evotec", [uri] "https://evotec.xyz", $true, "Tooltip for hyperlink", $false)

$Document.HyperLinks.Count

foreach ($HyperLink in $Document.HyperLinks) {
    if ($HyperLink.IsEmail) {
        Write-Host -Object "Email: $($HyperLink.EmailAddress)"
    } elseif ($HyperLink.IsHttp) {
        Write-Host -Object "URL: $($HyperLink.Uri)"
    } else {
        Write-Host -Object "Text: $($HyperLink.Text)"
    }
    # display properties of hyerlink
    $HyperLink | Format-Table

}

Save-OfficeWord -Document $Document -Show

This shows how to create new document with hyperlinks, and how to read them once created, but it would be similar when loading the document to just loop thru them and find it.

For searching text it will be similar. I do plan native Search & Replace that will be added in c# version and available in PowerShell later on but it's bit more complicated when you are searching for something that has a big mess when it comes to styling of a word. For example if each letter in a word would have different style it means .Text property contains a single letter and I somehow need to make it possible to search, merge and replace. My brain explodes when I think about it.

jmeldrum76 commented 2 years ago

This code example was extremely helpful! Do you have a code example much like the above that would search and replace text that is not a hyperlink?

PrzemyslawKlys commented 2 years ago
$Document = New-OfficeWord -FilePath $PSScriptRoot\Documents\BasicDocumentWithSomeText.docx
$Document.BuiltinDocumentProperties.Title = "This is title"
$Document.BuiltinDocumentProperties.Subject = "This is subject aka subtitle"

New-OfficeWordText -Document $Document -Text 'This is a test 1 ', 'and this should be bold' -Bold $null, $true -Underline Dash, $null

New-OfficeWordText -Document $Document -Text 'This is a test 2', ' and we continue using different color' -Color Blue, Gold -Alignment Right
New-OfficeWordText -Document $Document -Text 'This is a test 3 ', 'and this should be bold' -Bold $null, $true -Underline Dash, $null

New-OfficeWordText -Document $Document -Text 'This is a test 4', ' something else' -Color Green, Gold -Alignment Right
New-OfficeWordText -Document $Document -Text 'This is a test 5 ', 'and this should be bold' -Bold $null, $true -Underline Dash, $null

New-OfficeWordText -Document $Document -Text 'This is a test 6', ' even more text' -Color Blue, Gold -Alignment Right

foreach ($Paragraph in $Document.Paragraphs) {
    if ($Paragraph.Text) {
        if ($Paragraph.Text -like "*test 4*") {
            $Paragraph.Text = "We replace that value, but we keep the same color"
        }
    }
}

Save-OfficeWord -Document $Document -Show

This works. Basically you have to look at $Document as multi-level object that has all the information and then every (or most properties) can be used to get information or set it.

image

This shows you can easily find pagebreaks, fields, bookmarks, equations, lists and tables.

Also keep in mind that there are header and footers which also contain text if those are defined

image

Same goes for tables and so on. So depending where you're searching for text it may not be the straight-forward and multiple searches need to be made.

jmeldrum76 commented 2 years ago

Perfect, I will go a head and test it. Thank you so much!