【问题标题】:Save Clipboard image to .jpg将剪贴板图像保存为 .jpg
【发布时间】:2015-10-07 15:34:00
【问题描述】:

我有几个 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)

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

【问题讨论】:

    标签: excel powershell charts powerpoint


    【解决方案1】:

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

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

    有人知道如何解决上述两个问题吗?


    无论如何,我想出了一个解决方案,虽然不是很漂亮,但它现在正在工作,直到更好的解决方案。

    解决方案 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)以某种方式解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-21
      相关资源
      最近更新 更多