请示例代码:用python完成以下需求:截图当前屏幕并把截图名称命名为‘Tessellation’后将此截图插入DSTRESSTOOLGPU_Caps_Viewerbenchmark2023 acer video performance_test_criteria_V10_20220928xls的H20212223格内
您可以使用pyautogui库来实现截图和插入截图的需求。下面是一个示例代码:
import pyautogui
import os
# 截图当前屏幕
screenshot = pyautogui.screenshot()
# 定义截图保存路径和文件名
screenshot_path = "D:\STRESSTOOL\GPU_Caps_Viewer\benchmark"
screenshot_name = "Tessellation.png"
# 保存截图
screenshot.save(os.path.join(screenshot_path, screenshot_name))
# 导入所需的库
import openpyxl
from openpyxl.drawing.image import Image
# 打开Excel文件
excel_file = "D:\STRESSTOOL\GPU_Caps_Viewer\benchmark\2023 acer video performance_test_criteria_V1.0_20220928.xls"
workbook = openpyxl.load_workbook(excel_file)
# 获取指定工作表
worksheet = workbook.active
# 定义要插入截图的单元格范围
cell_range = ["H20", "H21", "H22", "H23"]
# 逐个插入截图
for cell in cell_range:
# 获取单元格
target_cell = worksheet[cell]
# 加载截图
image = Image(os.path.join(screenshot_path, screenshot_name))
# 调整截图大小以适应单元格
image.width = target_cell.width
image.height = target_cell.height
# 将截图插入单元格
target_cell.value = ""
target_cell.alignment = openpyxl.styles.Alignment(horizontal="center", vertical="center")
target_cell._comment = openpyxl.comments.Comment("截图", "Python Assistant")
target_cell.fill = openpyxl.styles.PatternFill(fill_type="solid", fgColor="D9D9D9")
target_cell.anchor = cell
worksheet.add_image(image, cell)
# 保存Excel文件
workbook.save(excel_file)
请确保在运行代码之前已经安装了pyautogui和openpyxl库。您可以使用以下命令来安装这两个库:
pip install pyautogui openpyxl
请注意,您需要根据您的实际文件路径和单元格范围进行相应的调整
原文地址: http://www.cveoy.top/t/topic/izuk 著作权归作者所有。请勿转载和采集!