首页 > 解决方案 > 搜索 word 文档 (.rtf) 并更改图像分辨率的脚本

问题描述

我需要一个在文件夹中搜索 word 文档 (.rtf) 并更改图像分辨率的脚本。问题是我有很多 .rtf 文件占用了大量空间,因为它们具有高分辨率图像。如果我更改图像的分辨率,文件会减少大约 97% 的空间。请帮我。

谢谢你。

标签: windowspowershellwindows-10word

解决方案


不幸的是,没有编程方式来执行“选择图像 > 图片格式 > 压缩图片”。可能值得设置一个 AutoHotKey 脚本来运行您的文件。


如果您的 rtf 文件最初是用 word 创建的,他们可能会保存每个图像的两个副本(原始文件和巨大的未压缩版本)。您可以通过在注册表中进行设置来更改此行为ExportPictureWithMetafile=0,然后重新保存每个文件。这可以通过脚本来完成,例如:

# Set registry key: (use the correct version number, mine is 16.0)
Try { Get-ItemProperty HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options\ -Name ExportPictureWithMetafile -ea Stop} 
Catch { New-ItemProperty HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options\ -Name ExportPictureWithMetafile -Value "0" | Out-Null }

# Get the list of files
$folder =  Get-ChildItem  "c:\temp\*.rtf" -File

# Set up save-as-filetype
$WdTypes = Add-Type -AssemblyName 'Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' -Passthru
$RtfFormat  = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatRTF

# Start Word
$word = New-Object -ComObject word.application
$word.Visible = $False

ForEach ($rtf in $folder) {
  # save as new name temporarily (otherwise word skips the shrink process)
  $doc = $word.documents.open($rtf.FullName)
  $TempName=($rtf.Fullname).replace('.rtf','-temp.rtf')
  $doc.saveas($TempName, $RtfFormat)
  
  # check for success, then delete original file
  # re-save to original name
  # check for success again, then clean up temp file
  if (Test-Path $TempName) { Remove-Item $rtf.FullName }
  $doc.saveas($rtf.FullName, $RtfFormat)
  if (Test-Path $rtf.FullName) { Remove-Item $TempName }
  
  # close the document
  $doc.SaveAs()
  $doc.close()
}

$word.quit()

我用 2mb 图像制作了一些默认的 word 文件,保存为 rtf(没有更改注册表),然后看到 rtf 文件是荒谬的 19mb!我运行了上面的脚本,它把它们缩小到了 5mb。


推荐阅读