python excel 截图 脚本

2022-05-13 09:29:24 浏览数 (2)

from win32com.client import Dispatch, DispatchEx import pythoncom from PIL import ImageGrab, Image import uuid

def excel_catch_screen(filename, sheetname, screen_area, img_name=False): """ 对excel的表格区域进行截图——用例:excel_catch_screen(ur"D:Desktop123.xlsx", "Sheet1", "A1:J10")""" pythoncom.CoInitialize() # excel多线程相关

代码语言:javascript复制
excel = DispatchEx("Excel.Application")  # 启动excel
excel.Visible = True  # 可视化
excel.DisplayAlerts = False  # 是否显示警告
wb = excel.Workbooks.Open(filename)  # 打开excel
ws = wb.Sheets(sheetname)  # 选择sheet
ws.Range(screen_area).CopyPicture()  # 复制图片区域
ws.Paste()  # 粘贴 ws.Paste(ws.Range('B1'))  # 将图片移动到具体位置

name = str(uuid.uuid4())  # 重命名唯一值
new_shape_name = name[:6]
excel.Selection.ShapeRange.Name = new_shape_name  # 将刚刚选择的Shape重命名,避免与已有图片混淆

ws.Shapes(new_shape_name).Copy()  # 选择图片
img = ImageGrab.grabclipboard()  # 获取剪贴板的图片数据
if not img_name:
    img_name = name   ".PNG"
img.save(img_name)  # 保存图片
wb.Close(SaveChanges=0)  # 关闭工作薄,不保存
excel.Quit()  # 退出excel
pythoncom.CoUninitialize()

if name == 'main': excel_catch_screen(r"C:UsershuangatDownloads2019-04-08_2019-04-21_yunying.xlsx", "Sheet", "A1:J6")

0 人点赞