如何使用python从excel中复制图表并将其作为图表粘贴到PowerPoint(而不是图像)中 [英] How to copy chart from excel and paste it as chart into powerpoint (not image) using python

查看:229
本文介绍了如何使用python从excel中复制图表并将其作为图表粘贴到PowerPoint(而不是图像)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EXCEL文件,它根据可用的数据生成图表,图表名称是thisChart

我要将thisChart从EXCEL文件复制到ppt文件。现在我知道了两种方法,即VBA和Python(使用win32com.client)。VBA的问题是它真的很耗时,而且它会随机崩溃,这需要持续的监控,因此我计划使用python来做同样的事情。

经过研究,我发现了python中的win32com.client,这让我可以做同样的事情。

我使用以下脚本执行此操作。


# Grab the Active Instance of Excel.
ExcelApp = win32com.client.GetActiveObject("Excel.Application")
ExcelApp.Visible = True

# Grab the workbook with the charts.
xlWorkbook = ExcelApp.Workbooks.Open(r'C:Usersprashant.kumarDesktop	estxl.xlsx')

# Create a new instance of PowerPoint and make sure it's visible.
PPTApp = win32com.client.gencache.EnsureDispatch("PowerPoint.Application")
PPTApp.Visible = True

# Add a presentation to the PowerPoint Application, returns a Presentation Object.
PPTPresentation = PPTApp.Presentations.Add()

# Loop through each Worksheet.
for xlWorksheet in xlWorkbook.Worksheets:

    # Grab the ChartObjects Collection for each sheet.
    xlCharts = xlWorksheet.ChartObjects()

    # Loop through each Chart in the ChartObjects Collection.
    for index, xlChart in enumerate(xlCharts):
        # Each chart needs to be on it's own slide, so at this point create a new slide.
        PPTSlide = PPTPresentation.Slides.Add(Index=index + 1, Layout=12)  # 12 is a blank layout

        # Display something to the user.
        print('Exporting Chart {} from Worksheet {}'.format(xlChart.Name, xlWorksheet.Name))

        # Copy the chart.
        xlChart.Copy()

        # Paste the Object to the Slide
        PPTSlide.Shapes.PasteSpecial(DataType=1)

    # Save the presentation.
PPTPresentation.SaveAs(r"C:Usersprashant.kumarDesktopoutppt")

但它将图表粘贴为图像,而我希望将图表粘贴为交互式图表(就像它在Excel文件中时一样)

原因是质量下降,我没有太大的灵活性可以在以后需要时对ppt中的图表进行细微的修改。

这里是两个输出的比较

这里可以看到质量差异,当我放大时,质量会变得更差。

现在我的问题是,有没有办法用python或任何比VBA更快的方式将图表从excel粘贴到ppt的图表格式?

PS。我不想读取excel文件数据并在python中生成图表,然后粘贴到PPT中,因为实际图表非常复杂,可能很难制作

推荐答案

尝试PPTSlide.Shapes.Paste,而不是现有代码中的PasteSpecial

这将以可编辑格式(而不是图片)复制并粘贴图表。

我也很努力,发现它很有用。

这篇关于如何使用python从excel中复制图表并将其作为图表粘贴到PowerPoint(而不是图像)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆