使用python-pptx嵌入Excel文件:add_ole_object

huangapple go评论81阅读模式
英文:

Embedding an excel file using python-pptx: add_ole_object

问题

我正在尝试将一个包含数据的Excel文件嵌入到PowerPoint文件中,但是当我运行代码时,它添加的是一个空的Excel文件,而不是具有数据的Excel文件(Test.xlsx)。

以下是我运行的代码:

from pptx import Presentation
from pptx.util import Inches 

ppt = Presentation()
title_slide = ppt.slides.add_slide(ppt.slide_layouts[0])
title_slide.shapes.add_ole_object('Test.xlsx', 'Excel.Sheet', left=Inches(1), top=Inches(3))
ppt.save('Test.pptx')

这是我参考的文档:

https://python-pptx.readthedocs.io/en/latest/api/shapes.html

我不确定我是否运行得不正确。我会感激任何关于此的建议。

英文:

I am trying to embed an excel file with data to a powerpoint file, but when I run the code, it adds an empty excel file instead of the excel file which has data (Test.xlsx).

Below is the code I ran,

from pptx import Presentation
from pptx.util import Inches 

ppt = Presentation()
title_slide = ppt.slides.add_slide(ppt.slide_layouts[0])
title_slide.shapes.add_ole_object('Test.xlsx', 'Excel.Sheet', left = Inches(1), top = Inches(3))
ppt.save('Test.pptx')

This is documentation I referred,

https://python-pptx.readthedocs.io/en/latest/api/shapes.html

I am unsure if I am running it incorrectly. I would appreciate any advice on this.

答案1

得分: 1

我已经解决了这个问题,所以对于 Prog_ID,我不再使用 'Excel.Sheet',而是使用 PROG_ID.XLSX。我使用 'from pptx.enum.shapes import PROG_ID' 导入了 PROG_ID。希望这对遇到这个问题的人有所帮助。

来源:github.com/scanny/python-pptx/issues/752

英文:

I was able to resolve this, so for the Prog_ID, instead of 'Excel.Sheet' I had to use PROG_ID.XLSX. I imported PROG_ID using 'from pptx.enum.shapes import PROG_ID'. In case anyone has this problem, hopefully this helps.

Source: github.com/scanny/python-pptx/issues/752

huangapple
  • 本文由 发表于 2023年8月9日 08:29:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定