我正在编写一个脚本来查看目录中的每个单词文档并删除所有图像。这是因为我有一个客户需要对大量文档进行去品牌化。
我编写了一个 powershell 脚本,它适用于大多数情况。然而,我遇到了一个文档,图像没有在 powershell 中注册为形状、内联形状、表格中的形状、文本框中的形状。我似乎无法让 powershell 将文档中的图像识别为图像,因此每次删除它时都会丢失它。
是否有另一种方法可以以编程方式从 Word 文档中删除所有图像,因为 powershell 中的“形状”内容似乎有一半时间不起作用。下面是我的代码:
`# 初始化一个变量来存储形状总数 $totalShapeCount = 0
# Loop through each story range in the document
foreach ($storyRange in $doc.StoryRanges) {
# Get the range of the current story
$currentRange = $storyRange
# Check if the current story range is not null
if ($currentRange -ne $null) {
# Loop through all shapes in the current story range and count them
$shapeCount = $currentRange.Shapes.Count
$totalShapeCount += $shapeCount
}
}
# Output the total number of shapes found in the entire document
Write-Host "Number of shapes found in the entire document: $totalShapeCount"
# Get the first page range
$firstPageHeight = $doc.PageSetup.PageHeight - $doc.PageSetup.TopMargin - $doc.PageSetup.BottomMargin
$firstPageRange = $doc.Range(0, $firstPageHeight)
# Output the range information for debugging
Write-Host "First Page Range Start: $($firstPageRange.Start)"
Write-Host "First Page Range End: $($firstPageRange.End)"
# Loop through all inline shapes in the first page
$inlineShapeCount = $firstPageRange.InlineShapes.Count
Write-Host "Number of inline shapes found on the first page: $inlineShapeCount"
# Loop through all shapes in the first page
$shapeCount = $firstPageRange.Shapes.Count
Write-Host "Number of shapes found on the first page: $shapeCount"
# Loop through all inline shapes in the first page
foreach ($shape in $firstPageRange.InlineShapes) {
# Check if the inline shape is an image
if ($shape.Type -eq 3) { # 3 corresponds to wdInlineShapePicture
# Delete the image
$shape.Delete()
Write-Host "Deleted inline image on the first page"
}
}
# Loop through all shapes in the first page and delete images
foreach ($shape in $firstPageRange.Shapes) {
if ($shape.Type -eq "msoPicture") {
$shape.Delete()
Write-Host "Deleted image on the first page"
}
}`
我尝试循环遍历文档中的每个内联形状、形状、文本框、表格,以查看其中是否有任何图像。由于调试中没有图像,一切都回来了。
正如评论所述,还有更多的形状和内联形状类型您可以考虑“图像”。
要检查某个形状是否具有您可能视为图像的类型,请将 foreach 循环更改为这样
# wdInlineShapePicture, wdInlineShapeLinkedPicture, wdInlineShapePictureHorizontalLine,
# wdInlineShapeLinkedPictureHorizontalLine, wdInlineShapePictureBullet,
# wdInlineShapeLockedCanvas, wdInlineShapeSmartArt, wdInlineShapeWebVideo
$inlineImages = 3,4,7,8,9,14,15,16
# Loop through all inline shapes in the first page
foreach ($shape in $firstPageRange.InlineShapes) {
# Check if the inline shape is an image
if ($shape.Type -in $inlineImages) {
# Delete the image
$shape.Delete()
Write-Host "Deleted inline image on the first page"
}
}
# or like this:
# $inlineImages = 3,4,7,8,9,14,15,16
# $firstPageRange.InlineShapes | Where-Object {$inlineImages -contains $_.Type} |
# ForEach-Object {
# $_.Delete()
# Write-Host "Deleted inline image on the first page"
# }
# msoLinkedPicture, msoPicture, msoMedia, msoCanvas, msoIgxGraphic,
# msoWebVideo, msoGraphic, msoLinkedGraphic
$msoImages = 11,13,16,20,24,26,28,29
# Loop through all shapes in the first page and delete images
foreach ($shape in $firstPageRange.Shapes) {
if ($shape.Type -in $msoImages) {
$shape.Delete()
Write-Host "Deleted image on the first page"
}
}
# or like this:
# $msoImages = 11,13,16,20,24,26,28,29
# $firstPageRange.Shapes | Where-Object {$msoImages -contains $_.Type} |
# ForEach-Object {
# $_.Delete()
# Write-Host "Deleted image on the first page"
# }