我需要一个脚本,在一个文件夹中搜索word文档(.rtf),并改变图像的分辨率。问题是我有很多.rtf文件,它们占用了很多空间,因为它们有高分辨率的图像。如果我改变图像的分辨率,文件会减少大约97%的空间。请帮帮我。
谢谢。
发布于 2021-08-27 17:49:05
不幸的是,没有编程的方法来做“选择图像>图片格式>压缩图片”。可能有必要设置一个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。
https://stackoverflow.com/questions/68940160
复制相似问题