python完成以下需求:将DSTRESSTOOLGPU_Caps_ViewerbenchmarkGL 21_Furry Cubepng以文件附件的形式插入到DSTRESSTOOLGPU_Caps_Viewerbenchmark2023 acer video performance_test_criteria_V10_20220928xls的excel的sheet名称为Benchmark Tool
你可以使用openpyxl库来读取和编辑Excel文件,使用PIL库来读取图像文件,并将图像插入到Excel文件中的特定单元格。
首先,确保你已经安装了openpyxl和PIL库。你可以使用以下命令来安装它们:
pip install openpyxl
pip install pillow
然后,你可以使用下面的代码来实现你的需求:
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
# 打开Excel文件
excel_file_path = r"D:\STRESSTOOL\GPU_Caps_Viewer\benchmark\2023 acer video performance_test_criteria_V1.0_20220928.xls"
wb = load_workbook(excel_file_path)
# 选择特定的工作表
sheet_name = 'Benchmark Tool'
ws = wb[sheet_name]
# 读取图像文件
image_file_path = r"D:\STRESSTOOL\GPU_Caps_Viewer\benchmark\GL 2.1_Furry Cube.png"
img = Image(image_file_path)
# 将图像插入到指定的单元格
cell_coordinate = 'G7'
ws.add_image(img, cell_coordinate)
# 保存修改后的Excel文件
wb.save(excel_file_path)
请确保文件路径的正确性,并将其替换为你实际的文件路径。此代码将把图像插入到Excel文件的特定单元格(7, 7)中
原文地址: https://www.cveoy.top/t/topic/iFZK 著作权归作者所有。请勿转载和采集!