欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > python采集监控数据

python采集监控数据

2025/2/22 16:32:28 来源:https://blog.csdn.net/qq_35583325/article/details/142959867  浏览:    关键词:python采集监控数据

1.采集监控数据,图片识别,替换xlsx数据

from PIL import ImageGrab
import time
import pyautogui
import openpyxl
import pytesseract# 设置 Tesseract 命令路径
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'def find_and_click(image_path, confidence):for attempt in range(3):  # 尝试3次image_location = pyautogui.locateOnScreen(image_path, grayscale=True, confidence=confidence)if image_location:time.sleep(0.5)position = pyautogui.center(image_location)pyautogui.click(position)#print(f"{image_path} 已成功点击")return  # 找到并点击后退出函数else:print(f"{image_path} 未找到指定的图片, 尝试 {attempt + 1}/3")time.sleep(1)  # 等待1秒后再尝试print(f"{image_path} 在3次尝试后仍未找到")def extract_text(image_path):try:# 打开图片im = Image.open(image_path)# 可选: 图像预处理im = im.convert('L')  # 转为灰度图im = im.point(lambda x: 0 if x < 128 else 255, '1')  # 二值化# 使用 Tesseract 进行文字识别text = pytesseract.image_to_string(im, lang='chi_sim')return textexcept Exception as e:print(f"处理图片 {image_path} 时出错: {e}")return ""time.sleep(3)
find_and_click("./images/03.jpg", 0.8)
time.sleep(1)#精密空调
pyautogui.moveTo(819,767)
time.sleep(1)
pyautogui.click()time.sleep(4)
# 定义截图区域的左上角和右下角坐标;这里的坐标应根据你的需求进行调整
left, top, right, bottom = 734,415, 785,448# 使用ImageGrab.grab()方法进行截图 ;参数为截图区域的左上角和右下角坐标
screenshot = ImageGrab.grab(bbox=(left, top, right, bottom))# 保存截图图片-温度
screenshot.save('./images/01jfhjwd-wd.png')time.sleep(1)left, top, right, bottom = 735,453, 779,485# 使用ImageGrab.grab()方法进行截图 ;参数为截图区域的左上角和右下角坐标
screenshot1 = ImageGrab.grab(bbox=(left, top, right, bottom))# 保存截图图片-湿度
screenshot1.save('./images/01jfhjwd-sd.png')time.sleep(1)# 导入相关包
from PIL import Image
import pytesseract#print('机房环境温度-截图')# 返回界面
find_and_click("./images/04.jpg", 0.8)
time.sleep(1)# 图片路径
p1 = './images/01jfhjwd-wd.png'
p2 = './images/01jfhjwd-sd.png'# 提取文字
text = extract_text(p1).replace('\n', '')
text1 = extract_text(p2).replace('\n', '')# 输出结果print("识别结果-空调温度:", text)
print("识别结果-空调湿度:", text1)# 打开 Excel 文件
file_path = 'test.xlsx'
workbook = openpyxl.load_workbook(file_path)# 查看所有工作表名称
#print("可用的工作表:", workbook.sheetnames)# 选择特定的工作表(假设要选择名为 'Sheet1' 的工作表)
sheet_name = '工作日志'  # 替换为你要选择的工作表名称
sheet = workbook[sheet_name]# 定义要查找和替换的文本
old_text = "空调运行是否正常:正常□  异常□,空调面板温度:  ℃;湿度:  Rh%。"
new_text = f"空调运行是否正常:正常□  异常□,空调面板温度:{text}℃;湿度:{text1}Rh%。"# 查找并替换
found = False
for row in sheet.iter_rows():for cell in row:if isinstance(cell.value, str) and old_text in cell.value:cell.value = cell.value.replace(old_text, new_text)found = True# 保存更改
if found:workbook.save(file_path)print("Excel 文件已更新。")
else:print("未找到匹配的文本。")

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词