How to copy chart from excel and paste it as chart into powerpoint (not image) using python(如何使用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(而不是图像)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用python从excel中复制图表并将其作为图表粘贴到PowerPoint(而不是图像)中


- 沿轴计算直方图 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01