win32com.client Dispatch pythoncom PIL shutil os
注意清空剪贴板os.system("echo off | clip")

from win32com.client import Dispatch, DispatchEx
import pythoncom
from PIL import ImageGrab, Image
import time, datetime, os, shutil
def excel_catch_screen(filename, sheetname, screen_area, img_name=False):
    os.system("echo off | clip")
    pythoncom.CoInitialize() 
    excel = DispatchEx("Excel.Application")
    excel.Visible = False
    excel.DisplayAlerts = False
    wb = excel.Workbooks.Open(filename)
    ws = wb.Sheets(sheetname)
    ws.Range(screen_area).CopyPicture()
    ws.Paste()
    new_shape_name=time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
    excel.Selection.ShapeRange.Name = new_shape_name
    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()
    pythoncom.CoUninitialize()
    os.system("echo off | clip")
    print(img_name)
 
if __name__ == '__main__':
    pass
    excel_catch_screen(r"C:\Users.xlsx", "sheet1", "C23:DI126",r"C:\Users.png")
    shutil.copyfile(r"C:\Users.png",r"Z:\www.png")
    print(time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) + ' - Screenshot Excel Done.')

标签: none

添加新评论