首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搜索word文档(.rtf)和更改图像分辨率的脚本

搜索word文档(.rtf)和更改图像分辨率的脚本
EN

Stack Overflow用户
提问于 2021-08-26 14:10:46
回答 1查看 29关注 0票数 0

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

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2021-08-27 17:49:05

不幸的是,没有编程的方法来做“选择图像>图片格式>压缩图片”。可能有必要设置一个AutoHotKey脚本来遍历您的文件。

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

代码语言:javascript
复制
# 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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68940160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档