使用Python从Excel到PowerPoint的图表

编程入门 行业动态 更新时间:2024-10-10 11:24:47
本文介绍了使用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的图表

本文发布于:2023-10-31 10:11:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545773.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图表   Python   Excel   PowerPoint

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!