直接上代码
运行后按F7是第一个坐标,F8是第二个坐标。可以获取F7和F8之前的信息
import pyautogui
from pynput.keyboard import Listener
from PIL import ImageGrab
from aip import AipOcr
# 百度识别文字
class FanYi(object):
# 用于存放坐标
coordinate = []
# 用于存放数据
data = []
def __init__(self, file, APP_ID, API_KEY, SECRET_KEY, num=0):
self.num = num
self.file = file
self.client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
if num != 0:
print("获取列数为"+str(num)+"的表格,鼠标移动到第一个坐标按F7开始。")
else:
pass
# 百度文字识别
def Ocr_baidu(self):
with open(self.file, 'rb') as f:
image = f.read()
text = self.client.basicAccurate(image)
words_result=text['words_result']
if self.num == 0:
for i in range(len(words_result)):
print(words_result[i]['words'])
else:
for i in range(len(words_result)):
data_in = words_result[i]['words']
self.data.append(data_in)
j = 1
str_data = ''
for i in self.data:
if j < self.num:
str_data = str_data+i+"\t"
j += 1
else:
j = 1
str_data = str_data+i+"\n"
self.data.clear()
print(str_data)
print("#####################鼠标移动到第一个坐标位置然后按F7########################")
# 执行截图
def make_screenshot(self, x1, y1, x2, y2):
bbox = (x1, y1, x2, y2)
im = ImageGrab.grab(bbox)
im.save(self.file)
def on_press(self, key):
pass
# 获取截图位置
def on_release(self, key):
print(str(key))
if str(key) == "Key.f7":
x1,y1 = pyautogui.position()
print("第一个坐标是:"+str(x1), str(y1)+",鼠标挪动到第二个坐标位置按F8开始获取信息")
self.coordinate.append(x1)
self.coordinate.append(y1)
elif str(key) == "Key.f8":
if len(self.coordinate) == 2:
x2,y2 = pyautogui.position()
print("第二个坐标是:"+str(x2), str(y2))
self.coordinate.append(x2)
self.coordinate.append(y2)
print(self.coordinate)
if self.coordinate[0] < self.coordinate[2] and self.coordinate[1] < self.coordinate[3]:
self.make_screenshot(self.coordinate[0],self.coordinate[1],self.coordinate[2],self.coordinate[3])
print("图片获取成功")
self.Ocr_baidu()
self.coordinate.clear()
elif self.coordinate[0] > self.coordinate[2] and self.coordinate[1] < self.coordinate[3]:
self.make_screenshot(self.coordinate[2],self.coordinate[1],self.coordinate[0],self.coordinate[3])
print("图片获取成功")
self.Ocr_baidu()
self.coordinate.clear()
elif self.coordinate[0] > self.coordinate[2] and self.coordinate[1] > self.coordinate[3]:
self.make_screenshot(self.coordinate[2],self.coordinate[3],self.coordinate[0],self.coordinate[1])
print("图片获取成功")
self.Ocr_baidu()
self.coordinate.clear()
elif self.coordinate[0] < self.coordinate[2] and self.coordinate[1] > self.coordinate[3]:
self.make_screenshot(self.coordinate[0],self.coordinate[3],self.coordinate[2],self.coordinate[1])
print("图片获取成功")
self.Ocr_baidu()
self.coordinate.clear()
else:
print("请按f7重新获取第一个坐标")
self.coordinate.clear()
else:
print("请按f7重新获取第一个坐标")
self.coordinate.clear()
else:
print("鼠标移动到第一个坐标位置然后按F7")
# 监控键盘f7和f8按钮
def start_listen(self):
with Listener(on_press=None, on_release=self.on_release) as listener:
listener.join()
# num=int(表格列数)默认等于0
set_num = input("如果是表格请输入表格的列数,如果不是回车跳过")
if set_num == '':
set_num = str(0)
FanYi = FanYi(file='find_cor_img.jpg',
APP_ID='baiduaip中获取',
API_KEY='9zA7FyHZCpkdkicY6NBG3aZ17',
SECRET_KEY='WD9n2SDPhqIe5W9WRY0nN8MLCvQU8ROU3',
num=int(set_num)
)
FanYi.start_listen()