Last active
July 29, 2023 13:14
-
-
Save yustier/c4cf45e9393e9a55916bbff4e9de3ba9 to your computer and use it in GitHub Desktop.
Word文書のパスワード付き編集保護を解除する(DeepLがかけたものとか!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Remove-DocumentProtectionFromAWordDocument { | |
param ( | |
[Parameter(Mandatory = $true, Position = 0)] | |
[string]$InputFilePath, | |
[Parameter(Mandatory = $false, Position = 1)] | |
[string]$OutputFilePath | |
) | |
# if (-not $InputFilePath) { | |
# Write-Host "Usage: Remove-DocumentProtectionFromAWordDocument input.docx" | |
# Write-Host "Usage: Remove-DocumentProtectionFromAWordDocument input.docx output.docx" | |
# return | |
# } | |
if (-not (Test-Path $InputFilePath)) { | |
Write-Error "The specified file does not exist." | |
return | |
} | |
if ([System.IO.Path]::GetExtension($InputFilePath) -ne ".docx") { | |
Write-Error "The specified file is not a word document." | |
return | |
} | |
if (-not $OutputFilePath) { | |
$OutputFilePath = "out.docx" | |
} | |
$tempDir = [System.IO.Path]::GetTempPath() + [System.IO.Path]::GetRandomFileName() | |
New-Item -ItemType Directory -Force -Path $tempDir | Out-Null | |
$tempDir = $tempDir + "\" | |
Expand-Archive -Path $InputFilePath -DestinationPath $tempDir -Force | Out-Null | |
$wordSettingsXmlPath = $tempDir + "word\settings.xml" | |
$wordSettingsXml = [xml](Get-Content $wordSettingsXmlPath) | |
$wordSettingsXml ` | |
| Select-Xml -Namespace @{w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" } -XPath "//w:documentProtection" ` | |
| ForEach-Object { | |
$_.Node.ParentNode.RemoveChild($_.Node) | |
} | Out-Null | |
$wordSettingsXml.Save($wordSettingsXmlPath) | |
Compress-Archive -Path $tempDir* -DestinationPath $OutputFilePath -Force | Out-Null | |
Remove-Item -Path $tempDir -Force -Recurse | Out-Null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment