简体   繁体   English

使用Powershell和Word 2013在文档中保存图片

[英]Save pictures in document using Powershell and Word 2013

I'm trying to convert a HTML file with externally linked images to RTF using MS Word 2013 via Powershell commands, when using below command file gets converted however pictures are missing 我试图通过Powershell命令使用MS Word 2013将带有外部链接图像的HTML文件转换为RTF,当使用下面的命令文件时会转换,但是图片丢失

$wrd = New-Object -ComObject "Word.Application"
$doc = $wrd.Documents.Open('c:\test.html')
$opt = [ref][Microsoft.Office.Interop.Word.WdSaveFormat]::WdFormatRTF
$name= [ref]'C:\test.rtf'
$wrd.ActiveDocument.SaveAs($name, $opt)
$wrd.ActiveDocument.Close()
$wrd.Quit()

If I do the conversion manually by opening the HTML file with Word and saving it as RTF same thing happens, no images, however if within Word I go to File, click on "Edit Links to File" and highlight all images, tick "Save picture in document" and then click on "Break Link" and then do the save as RTF, this time images are present within RTF (however with bad quality which is another issue..) [See picture below] 如果我通过使用Word打开HTML文件并将其另存为RTF来手动进行转换,则不会发生任何图像,但是,如果在Word中进入文件,请单击“编辑链接到文件”并突出显示所有图像,然后勾选“保存”图片“文档中的图片”,然后单击“断开链接”,然后另存为RTF,这次图像存在于RTF中(但是质量较差,这是另一个问题。)。[请参见下图] 在此处输入图片说明

Is there a way to execute above process within Powershell? 有没有办法在Powershell中执行上述过程?

Thanks 谢谢

This does the job 这做好了

    $images = $doc.InlineShapes
    foreach ($image in $images) {
      $linkFormat = $image.LinkFormat
      $linkFormat.SavePictureWithDocument = 1
      $linkFormat.BreakLink()
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM