python实现工作巡检截图
全程坐标定位
源码展示,欢迎指导
# coding:utf-8
'研发云自动截图--ybt'
import os
import time
from io import BytesIO
from time import sleep as sl
import pyautogui as gui
import pyperclip
from PIL import Image, ImageGrab
from win32 import win32clipboard
# 截图性能
def imxn(pr, s):
k = ImageGrab.grab((269, 227, 1906, 409))
k.save(r'C:\ybt\%s\ybt%i.jpg' % (pr, s))
# 截图并发
def imbf(pr, s):
k = ImageGrab.grab((269, 234, 1074, 513))
k1 = ImageGrab.grab((1088, 232, 1905, 513))
k.save(r'C:\ybt\%s\ybtLbf%i.jpg' % (pr, s))
k1.save(r'C:\ybt\%s\ybtRbf%i.jpg' % (pr, s))
# cds截图
def imcds(pr):
k = ImageGrab.grab((265, 231, 1905, 598))
k.save(r'C:\ybt\%s\ybtCds.jpg' % pr)
# nginx截图
def imnginx(pr):
k = ImageGrab.grab((275, 517, 1893, 959))
k.save(r'C:\ybt\%s\ybtNginx1.jpg' % pr)
# 向下移动
def down():
gui.keyDown('down')
gui.keyUp('down')
# 点击设置
def click(a, b, s=1, sls=1):
gui.moveTo(a, b)
gui.click(clicks=s)
sl(sls)
# 开始按钮
text = ['截图即将开始,请选择:开始/终止\n\n反向键左右操控即可', '贴图即将开始,请选择:开始/终止\n\n反向键左右操控即可']
def begin(a):
sf = gui.confirm(text='%s' % a, title='ybt-提醒', buttons=['开始', '终止'])
if sf == '开始':
pass
else:
exit()
# 初始化鼠标
def initMouse():
gui.moveTo(1899, 295) # 1918, 295
gui.scroll(10000)
gui.click(clicks=1)
# 将图片拷贝至粘贴板并粘贴
def copyPaste(jpg):
im = Image.open(jpg) # 打开图片
op = BytesIO()
im.save(op, 'BMP')
data = op.getvalue()[14:]
op.close()
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
win32clipboard.CloseClipboard()
gui.hotkey('ctrl', 'v')
sl(1.6)
down()
# 判断目录是否存在和清除缓存
def directory():
# 如果没有各省目录将创建目录
if os.path.exists(r'C:\ybt\ln'):
pass
else:
for i in ['ln', 'nm', 'ha', 'ah', 'hb', 'ly']:
os.makedirs(r'C:\ybt\%s' % i)
# 删除目录缓存文件
anyFile = ['ln', 'nm', 'ha', 'ah', 'hb']
for x in anyFile:
for i in os.listdir(r'C:\ybt\%s' % x):
os.remove(r'C:\ybt\%s\%s' % (x, i))
# 省份与时间自动选择
def switchTheProvinces(a, pr):
gui.moveTo(680, 132)
sl(1)
gui.hotkey('ctrl', 'F5')
gui.hotkey('enter')
sl(7)
click(73, 455, sls=1.5)
click(76, 566, sls=1.5)
click(276, 284, sls=1.5)
click(315, 211, 2, 0)
pyperclip.copy('%s分公司巡检' % a)
gui.hotkey('ctrl', 'v')
sl(5)
down()
gui.hotkey('enter')
sl(5)
click(1828, 210)
click(1307, 289, 1)
gui.hotkey('ctrl', 'a', 'backspace')
sl(0.5)
pyperclip.copy('now-3d')
gui.hotkey('ctrl', 'v')
click(1281, 336, 1, 0)
gui.hotkey('ctrl', 'a', 'backspace')
sl(0.5)
pyperclip.copy('now')
gui.hotkey('ctrl', 'v')
sl(0.5)
gui.hotkey('enter')
sl(0.5)
if pr == 'hb':
click(305, 430, sls=0.5)
else:
click(290, 395, sls=0.5)
if pr == 'hb':
click(308, 368, sls=0.5)
else:
click(300, 327, sls=0.5)
if pr == 'hb':
click(309, 331, sls=0.5)
else:
click(317, 299, sls=0.5)
sl(90)
# 截图住循环
def performanceErupt(pr, all=None):
# 初始化
if all == 'all':
pass
else:
begin(text[0])
initMouse()
if pr == 'hb':
down()
# 移动yfy表头
for i in range(4):
down()
s = 0
# 性能截图主循环
while s < 9:
sl(0.15)
imxn(pr, s)
for i in range(8):
down()
if s == 5:
if pr == 'hb':
sl(0.15)
imxn(pr, s + 100)
for i in range(8):
down()
if s == 6:
sl(0.15)
imcds(pr)
for i in range(8):
down()
s += 1
# 并发截图主循环
a = 10
down()
while a < 17:
sl(0.15)
imbf(pr, a)
for i in range(12):
down()
if a == 16:
for i in range(12):
down()
if pr == 'hb':
for i in range(12):
down()
sl(0.15)
imnginx(pr)
a += 1
# 4大省贴图循环
def chartletSs(coordinatex, coordinateY, pr, all=None):
if all == 'all':
pass
else:
begin(text[1])
gui.moveTo(1933, 295)
gui.click(clicks=1)
gui.scroll(2000)
# 性能主循环
x = 0 # 三十个格子
while x < 24:
if x < 4:
if x == 0:
gui.moveTo(coordinatex, coordinateY)
gui.click(clicks=1)
copyPaste(r'C:\ybt\%s\ybt%i.jpg' % (pr, x))
if x == 4:
copyPaste(r'C:\ybt\%s\ybt5.jpg' % pr)
if x == 5 or x == 6:
down()
if x == 7:
copyPaste(r'C:\ybt\%s\ybt6.jpg' % pr)
if x == 8:
copyPaste(r'C:\ybt\%s\ybtCds.jpg' % pr)
if x == 9:
copyPaste(r'C:\ybt\%s\ybt8.jpg' % pr)
if x >= 10:
if x < 13:
copyPaste(r'C:\ybt\%s\ybtLbf%i.jpg' % (pr, x))
copyPaste(r'C:\ybt\%s\ybtRbf%i.jpg' % (pr, x))
if x == 13 or x == 14 or x == 21 or x == 22:
down()
if x == 15 or x == 18 or x == 23:
copyPaste(r'C:\ybt\%s\ybtNginx1.jpg' % pr)
if x == 16:
copyPaste(r'C:\ybt\%s\ybtLbf13.jpg' % pr)
if x == 17:
copyPaste(r'C:\ybt\%s\ybtRbf13.jpg' % pr)
if x == 19 or x == 20:
copyPaste(r'C:\ybt\%s\ybtLbf%s.jpg' % (pr, str(x - 5)))
copyPaste(r'C:\ybt\%s\ybtRbf%s.jpg' % (pr, str(x - 5)))
x += 1
# 湖北贴图循环
def chartlet_Hb(coordinatex, coordinateY, pr, all=None):
if all == 'all':
pass
else:
begin(text[1])
gui.moveTo(1933, 295)
gui.click(clicks=1)
gui.scroll(2000)
# 性能主循环
x = 0 # 三十个格子
while x < 24:
if x < 4:
if x == 0:
gui.moveTo(coordinatex, coordinateY)
gui.click(clicks=1)
copyPaste(r'C:\ybt\%s\ybt%i.jpg' % (pr, x))
if x == 4:
copyPaste(r'C:\ybt\%s\ybt5.jpg' % pr)
if x == 5:
down()
if x == 6:
copyPaste(r'C:\ybt\%s\ybt105.jpg' % pr)
if x == 7:
copyPaste(r'C:\ybt\%s\ybt6.jpg' % pr)
if x == 8:
copyPaste(r'C:\ybt\%s\ybtCds.jpg' % pr)
if x == 9:
copyPaste(r'C:\ybt\%s\ybt8.jpg' % pr)
if x >= 10:
if x < 13:
copyPaste(r'C:\ybt\%s\ybtLbf%i.jpg' % (pr, x))
copyPaste(r'C:\ybt\%s\ybtRbf%i.jpg' % (pr, x))
if x == 13 or x == 21 or x == 22:
down()
if x == 14:
if pr == 'hb':
copyPaste(r'C:\ybt\%s\ybtLbf13.jpg' % pr)
else:
down()
if x == 15 or x == 18 or x == 23:
copyPaste(r'C:\ybt\%s\ybtNginx1.jpg' % pr)
if x == 16:
if pr == 'hb':
copyPaste(r'C:\ybt\%s\ybtRbf13.jpg' % pr)
else:
copyPaste(r'C:\ybt\%s\ybtLbf13.jpg' % pr)
if x == 17:
copyPaste(r'C:\ybt\%s\ybtLbf14.jpg' % pr)
if x == 19 or x == 20:
copyPaste(r'C:\ybt\%s\ybtRbf%s.jpg' % (pr, str(x - 5)))
copyPaste(r'C:\ybt\%s\ybtLbf%s.jpg' % (pr, str(x - 4)))
x += 1
province = {'ln': '辽宁', 'nm': '内蒙古', 'ha': '河南', 'ah': '安徽', 'hb': '湖北'}
provinceTwo = ['ln', 'nm', 'ha', 'ah']
b = 94 # 格子宽度移动常量
# 标题选择与循环
def beginProvince():
directory()
while True:
sf = gui.confirm(text='请选择省份', title='ybt-提醒',
buttons=['ln', 'nm', 'ha', 'ah', 'hb', 'all', 'ly', 'exit'])
if sf == 'ln':
performanceErupt('ln')
chartletSs(195, 356, 'ln')
continue
if sf == 'nm':
performanceErupt('nm')
chartletSs(195 + b, 356, 'nm')
continue
if sf == 'ha':
performanceErupt('ha')
chartletSs(195 + b * 2, 356, 'ha')
continue
if sf == 'ah':
s = time.perf_counter()
performanceErupt('ah')
s1 = time.perf_counter()
print(s - s1)
chartletSs(195 + b * 3, 356, 'ah')
continue
if sf == 'hb':
performanceErupt('hb')
chartlet_Hb(195 + b * 4, 356, 'hb')
continue
if sf == 'all':
# 截图
for i in province:
switchTheProvinces(province[i], i)
performanceErupt(i, 'all')
# 贴图
os.system('start 要打开石墨excel网址')
sl(4)
for i in range(4):
chartletSs(195 + b * i, 356, provinceTwo[i], 'all')
chartlet_Hb(195 + b * 4, 356, 'hb', 'all')
if sf == 'ly':
gui.alert(text='洛阳资源池自己截图吧', title='ybt-提醒', button='OK')
exit()
if sf == 'exit':
exit()
else:
exit()
if __name__ == "__main__":
beginProvince()