You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell移除Word文档图片失效,求通用程序化解决方案

批量移除Word文档图片的通用PowerShell方案求助

我正在编写PowerShell脚本,遍历目录下所有Word文档以移除图片,帮助客户完成大批量文档的去品牌化操作。该脚本对多数文档有效,但遇到某份文档时,其中的图片无法被识别为Shape、InlineShape、表格或文本框内的图形,调试结果显示未检测到图片,导致无法移除。现寻求可程序化移除Word文档所有图片的通用方法。

以下是当前使用的代码:

# Initialize a variable to store the total shape count
$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"
    }
}

我已尝试遍历文档中所有InlineShape、Shape、文本框及表格以查找图片,但调试均显示未找到图片。

内容的提问来源于stack exchange,提问作者DanielSCG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 19:27:19