python 怎么把图片插入到指定单元格
可以使用 openpyxl 库来实现在指定单元格插入图片的功能。具体步骤如下:
- 导入 openpyxl 库和 Image 类:
 
from openpyxl import Workbook
from openpyxl.drawing.image import Image
- 创建 Workbook 对象和 Worksheet 对象:
 
wb = Workbook()
ws = wb.active
- 加载图片文件:
 
img = Image('image.png')
- 插入图片到指定单元格:
 
ws.add_image(img, 'A1')
其中,'A1' 表示指定单元格的位置,可以根据需要修改。
- 保存文件:
 
wb.save('example.xlsx')
完整代码示例:
from openpyxl import Workbook
from openpyxl.drawing.image import Image
wb = Workbook()
ws = wb.active
img = Image('image.png')
ws.add_image(img, 'A1')
wb.save('example.xlsx')
``
原文地址: https://www.cveoy.top/t/topic/egAX 著作权归作者所有。请勿转载和采集!