将剪贴板图像保存为.jpg

我有几个Excel报告,并需要导出选定的图表作为图像,作为第一步。 然后我需要将这些图像导入到PowerPoint演示文稿中。 看来我几乎在那里,但卡住了。

  1. 我试图用方法chartObjects获得工作表的所有图表(我已经试过了方法图表,但是我的集合是空的)。

然后,我将所选的图表复制到剪贴板(当这个工作,我将使用forEach获得我的工作簿的所有图表)。

但是,当我试图保存剪贴板数据时,似乎没有图片,而有数据。

$xl = new-object -c excel.application $xl.displayAlerts = $false $xl.visible = $false $wb = $xl.workbooks.open($fileData) $ws= $wb.sheets.Item("Sheet1") $chartCollect = $ws.chartObjects() $chartToExport = $chartCollect.Item(3) $toClipboard = $charToExport.copyPicture() $e=[System.Windows.Forms.Clipboard]::GetDataObject() if ($e -eq $null) { Write-Host "there is no data" } $img =$e.GetImage() if ($img -eq $null) { Write-Host "there is no picture" } $img.save("Path\test.jpg", "jpg") # I tried using the export method, but it does not exist with chartObjects $a = $chartToExport.Export("Path\test.gif", "GIF") 
  1. 跳过第1步,我试图直接将剪贴板数据放到PowerPoint演示文稿中,该演示文稿正在工作。

但是,我不能按照我的意愿放置导入的图片。 我在同一时间试图导入图片的文件夹,然后我没有麻烦玩他们的位置。

  $pptx = "Path\Presentation.pptx" $pt = New-Object -ComObject powerpoint.application $presentation = $pt.Presentations.open($pptx) $slide2 = $presentation.slides.item(2) $slide3 = $presentation.slides.item(3) # Paste the Clipboard chart into the PowerPoint presentation. # Works OK, but I do not success to place where I wish $shape2 = $slide2.shapes.paste() $shape2.Left = 10 # Does not work $shape2.Top = 10 # Does not work # Test on existing picture, Works OK $imgTest = "Path\picture.jpg" $LinkToFile = $msoFalse $SaveWithDocument = $msoTrue $Left = 10 $Top = 10 $Width = 10 $Height = 10 $shape3 = $slide3.shapes.addPicture($imgPath, $LinkToFile, $SaveWithDocument,$Left, $Top, $Width, $Height) 

有谁知道如何解决这些问题或两者之一?

我发现剪贴板2)导入一个.emf文件。 我试图玩1)中的.copyPicture方法来导出.jpg格式,但没有成功。 (使用: copyPicture方法说明 )

我也试着按照incrementLeft和incrementTop方法来修改导入的.emf文件的位置,但没有成功(使用: incrementLeft方法 )。 错误是:使用“1”参数调用“incrementLeft”的exception:来自HRESULT:0x800A01A8的exception

有谁知道如何解决以上两个问题?

无论如何,我提出了一个解决scheme,而不是美丽的,但现在正在工作,直到更好。

解决scheme2:

在导入剪贴板图片之前,我复制了目标幻灯片。

然后导入剪贴板,然后将.emf文件导出到.jpg文件。

我擦除重复的幻灯片并导入.jpg新创build的文件。 我成功了,然后修改他导入的.jpg的位置,而我用.emf对象不能这么做。

  $slide2 = $presentation.slides.item(2) $slide2Dup = $slide2.duplicate() $shape2 = $slide2Dup.shapes.paste() $a= $shape2.export("Path\export.jpg", $ppShapeFormatJPG) $slide2Dup.delete() $imgPath = 'Path\export.jpg' $LinkToFile = $msoFalse $SaveWithDocument = $msoTrue $Left = 10 $Top = 10 $Width = 10 $Height = 10 $shape2 = $slide2.shapes.addPicture($imgPath, $LinkToFile, $SaveWithDocument,$Left, $Top, $Width, $Height) 

同时,得到一个与excel表对应的.jpg文件的文件夹,所以1)被解决了。