将剪贴板图像保存为.jpg

时间:2015-10-07 15:34:01

标签: excel powershell charts powerpoint

我有几个Excel报告,并且需要将所选图表导出为图像,这是第一步。然后,我需要将这些图像导入PowerPoint演示文稿。 我好像差不多了,但还是卡住了。

  1. 我试图用方法chartObjects获取工作表的所有图表(我已尝试使用方法图表,但后来我的收集是空的)。
  2. 然后我将选中的图表作为图片复制到剪贴板中(当这将工作时,我将使用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演示文稿中,这是有效的。
    2. 但是,我无法按照自己的意愿放置导入的图片。我试图在同一时间导入文件夹的图片,然后我就没有麻烦玩他们的位置。

          $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)
      

      有谁知道如何解决其中一个或两个问题?

1 个答案:

答案 0 :(得分:0)

我发现剪贴板粘贴在2)导入.emf文件。我试图在1)中使用.copyPicture方法来导出.jpg格式,但没有成功。 (使用:copyPicture method description

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

有谁知道如何解决上述2个问题?


无论如何,我想出了一个解决方案,不是很漂亮,但现在一直在努力,直到更好。

解决方案2:

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

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

删除重复的幻灯片并导入.jpg新创建的文件。我成功地修改了导入.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)以某种方式解决了。