python完成以下需求:将DSTRESSTOOLGPU_Caps_ViewerbenchmarkGL 21_Furry Cubepng此文件以附件的形式插入到DSTRESSTOOLGPU_Caps_Viewerbenchmark2023 acer video performance_test_criteria_V10_20220928xls的excel的sheet名称为Benchmark Too
你可以使用openpyxl库来操作Excel文件,PIL库来处理图片,以下是使用这两个库完成你的需求的示例代码:
from openpyxl import Workbook
from openpyxl.drawing.image import Image
from PIL import Image as PILImage
# 打开Excel文件
workbook = Workbook()
sheet = workbook.active
sheet.title = 'Benchmark Tool'
# 加载图片
image_path = r"D:\STRESSTOOL\GPU_Caps_Viewer\benchmark\GL 2.1_Furry Cube.png"
image = PILImage.open(image_path)
# 将图片插入到Excel中的指定单元格
cell = sheet.cell(row=7, column=7)
image_object = Image(image)
cell.anchor = 'A1'
sheet.add_image(image_object, cell.coordinate)
# 保存Excel文件
excel_path = r"D:\STRESSTOOL\GPU_Caps_Viewer\benchmark\2023 acer video performance_test_criteria_V1.0_20220928.xls"
workbook.save(excel_path)
请注意,openpyxl库只支持对.xlsx文件的读写操作,不支持对.xls文件的直接写操作。上述代码会将图片插入到一个新创建的.xlsx文件中,如果需要将图片插入到一个已存在的.xls文件中,你需要首先将.xls文件另存为.xlsx格式,然后再使用上述代码操作
原文地址: https://www.cveoy.top/t/topic/iF4q 著作权归作者所有。请勿转载和采集!