python 取excel中的图片拷贝到ppt
时间: 2023-07-26 10:18:36 浏览: 76
您好!针对您的问题,您可以使用Python中的`openpyxl`和`python-pptx`库来实现。具体步骤如下:
1. 使用`openpyxl`库读取Excel文件中的图片,并将图片保存到本地。
```python
from openpyxl import load_workbook
# 打开 Excel 文件
wb = load_workbook('example.xlsx')
# 获取 sheet
sheet = wb['Sheet1']
# 获取图片,并保存到本地
for img in sheet._images:
img_path = f'image_{img.id}.jpg' # 生成图片保存路径
with open(img_path, 'wb') as f:
f.write(img._data)
```
2. 使用`python-pptx`库创建一个PPT文件,并将图片插入到PPT中。
```python
from pptx import Presentation
from pptx.util import Inches
# 创建一个 PPT 文件
prs = Presentation()
# 创建一个新的幻灯片
slide = prs.slides.add_slide(prs.slide_layouts[0])
# 将图片插入到幻灯片中
left = top = Inches(1)
for img_path in ['image_1.jpg', 'image_2.jpg']:
pic = slide.shapes.add_picture(img_path, left, top)
left += Inches(5) # 指定图片之间的间距
# 保存 PPT 文件
prs.save('example.pptx')
```
这样就可以将 Excel 文件中的图片拷贝到 PPT 文件中了。希望对您有所帮助!