我有一个使用出色的“xlsxwriter”模块创建的excel工作簿。在此工作簿中,大约有200个嵌入式图表。我现在正在尝试将所有这些图表导出到几个Powerpoint演示文稿中。理想情况下,我想保留原始格式和嵌入式数据,而不链接到外部Excel工作簿。
我确信有一种使用VBA做到这一点的方法。但是,我想知道是否有一种方法可以使用Python做到这一点。有没有办法将xlsxwriter图表对象放入PowerPoint中?
我看过python-pptx,找不到关于从Excel工作簿获取图表或数据系列的任何信息。
任何帮助表示赞赏!
最佳答案
经过数小时的尝试,我找到了解决该问题的方法。希望它可以帮助某人节省一些时间。下面的代码会将所有图表从“workbook_with_charts.xlsx”复制到“Final_PowerPoint.pptx”。
出于某种原因(我尚不了解),当从CMD终端运行此Python程序时,它会更好地工作。即使您尝试多次运行,有时它也会崩溃,即使第一次运行通常可以。
另一个问题是,在第五行中,如果使用“presentation = PowerPoint.Presentations.Add(False)”使False,则即使“True”和“False”仍可与Microsoft Office 2013一起使用,它也无法使用。 Microsoft Office 2010。
如果有人可以就两个问题澄清这些,那就太好了。
# 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从Excel到PowerPoint的图表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32639900/