使用Python从Excel到PowerPoint的图表 [英] Charts from Excel to PowerPoint with Python

查看:492
本文介绍了使用Python从Excel到PowerPoint的图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用出色的 xlsxwriter模块创建的excel工作簿。在此工作簿中,大约有200个嵌入式图表。我现在正在尝试将所有这些图表导出到几个Powerpoint演示文稿中。理想情况下,我想保留原始格式和嵌入式数据,而不链接到外部excel工作簿。

I have an excel workbook that is created using an excellent "xlsxwriter" module. In this workbook, there about about 200 embedded charts. I am now trying to export all those charts into several power point presentations. Ideally, I want to preserve the original format and embedded data without linking to external excel work book.

我敢肯定有一种使用VBA做到这一点的方法。但是,我想知道是否有办法使用Python做到这一点。有没有办法将xlsxwriter图表对象放入PowerPoint中?

I am sure there is a way to do this using VBA. But, I was wondering if there is a way to do this using Python. Is there a way to put xlsxwriter chart objects into powerpoints ?

我看过python-pptx,找不到关于从excel工作簿中获取图表或数据系列的任何信息。

I have looked at python-pptx and can't find anything about getting charts or data series from excel work book.

感谢您的帮助!

推荐答案

花了数小时尝试其他事情之后,我找到了解决该问题的方法。希望可以帮助某人节省一些时间。以下代码会将所有图表从 workbook_with_charts.xlsx复制到 Final_PowerPoint.pptx。

After spending hours of trying different things, I have found the solution to this problem. Hopefully,it will help someone save some time.The following code will copy all the charts from "workbook_with_charts.xlsx" to "Final_PowerPoint.pptx."

我还不了解,从CMD终端运行此Python程序时效果更好。即使您尝试多次运行,有时它也会崩溃,即使第一次运行通常可以。

For some reason, that I am yet to understand, it works better when running this Python program from CMD terminal. It sometimes breaks down if you tried to run this several times, even though the first run is usually OK.

另一个问题是,在第五行中,如果使用 presentation = PowerPoint.Presentations.Add(False)使False,则它不适用于Microsoft Office 2013 ,即使 True和 False仍然可以在Microsoft Office 2010上使用。

Another issue is that in the fifth line, if you make False using "presentation=PowerPoint.Presentations.Add(False)," it does not work with Microsoft Office 2013, even though both "True" and "False" will still work with Microsoft Office 2010.

如果有人可以澄清两个问题,那就太好了。

It would be great if someone can clarify these about two issues.

# importing the necessary libraries
import win32com.client
from win32com.client import constants

PowerPoint=win32com.client.Dispatch("PowerPoint.Application")
Excel=win32com.client.Dispatch("Excel.Application")


presentation=PowerPoint.Presentations.Add(True)
workbook=Excel.Workbooks.Open(Filename="C:\\.........\\workbook_with_charts.xlsx",ReadOnly=1,UpdateLinks=False)

for ws in workbook.Worksheets:
    for chart in ws.ChartObjects():
    # Copying all the charts from excel
        chart.Activate()
        chart.Copy()  

        Slide=presentation.Slides.Add(presentation.Slides.Count+1,constants.ppLayoutBlank)
        Slide.Shapes.PasteSpecial(constants.ppPasteShape)

    # WE are going to make the title of slide the same chart title
    # This is optional 
    textbox=Slide.Shapes.AddTextbox(1,100,100,200,300)
    textbox.TextFrame.TextRange.Text=str(chart.Chart.ChartTitle.Text)

presentation.SaveAs("C:\\...........\\Final_PowerPoint.pptx")
presentation.Close()
workbook.Close()

print 'Charts Finished Copying to Powerpoint Presentation'

Excel.Quit()
PowerPoint.Quit()

这篇关于使用Python从Excel到PowerPoint的图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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