import pdfplumber import openpyxl import os import time from PySide6.QtCore import Signal, QThread from codes.common import clibs class IsoDataProcess(QThread): output = Signal(str, str) def __init__(self, dir_path, /): super().__init__() self.dir_path = dir_path self.idx = 2 def logger(self, level, module, content, color="black", error="", flag="both"): flag = "cursor" if level.upper() == "DEBUG" else "both" clibs.logger(level, module, content, color, flag, signal=self.output) if level.upper() == "ERROR": raise Exception(f"{error} | {content}") def p_iso(self, file, p_files, ws, tmpfile): p_files.append(file) pdf = pdfplumber.open(file) with open(tmpfile, mode="w", encoding="utf-8") as fb: for page in pdf.pages: fb.write(page.extract_text()) with open(tmpfile, mode="r", encoding="utf-8") as fb: lines = fb.readlines() lines = [line for line in lines if not line.startswith("Page ")] for line in lines: if line.strip() == "Pose Accuracy and Repeatability": index = lines.index(line) ws.cell(row=3, column=7).value = float(lines[index+4].split()[1]) ws.cell(row=4, column=7).value = float(lines[index+5].split()[1]) ws.cell(row=5, column=7).value = float(lines[index+6].split()[1]) ws.cell(row=6, column=7).value = float(lines[index+7].split()[1]) ws.cell(row=7, column=7).value = float(lines[index+8].split()[1]) ws.cell(row=8, column=7).value = float(lines[index+4].split()[2]) ws.cell(row=9, column=7).value = float(lines[index+5].split()[2]) ws.cell(row=10, column=7).value = float(lines[index+6].split()[2]) ws.cell(row=11, column=7).value = float(lines[index+7].split()[2]) ws.cell(row=12, column=7).value = float(lines[index+8].split()[2]) elif line.strip() == "Pose Accuracy Variation": index = lines.index(line) ws.cell(row=13, column=7).value = float(lines[index+4].split()[1]) ws.cell(row=14, column=7).value = float(lines[index+5].split()[1]) ws.cell(row=15, column=7).value = float(lines[index+6].split()[1]) elif line.strip() == "Distance Accuracy": index = lines.index(line) ws.cell(row=16, column=7).value = float(lines[index + 4].split()[1]) ws.cell(row=17, column=7).value = float(lines[index + 4].split()[2]) elif line.strip() == "Stabilisation Time and Overshoot": index = lines.index(line) ws.cell(row=18, column=7).value = float(lines[index + 7].split()[3]) ws.cell(row=19, column=7).value = float(lines[index + 7].split()[2]) elif line.strip() == "Velocity Accuracy and Repeatability": index = lines.index(line) ws.cell(row=20, column=7).value = float(lines[index + 4].split()[1]) ws.cell(row=21, column=7).value = float(lines[index + 4].split()[2]) ws.cell(row=22, column=7).value = float(lines[index + 4].split()[3]) elif line.strip()[:31] == "Path Accuracy and Repeatability": index = lines.index(line) ws.cell(row=29, column=7).value = float(lines[index + 4].split()[1]) ws.cell(row=30, column=7).value = float(lines[index + 4].split()[2]) elif line.strip() == "Corner Overshoot and Roundoff": index = lines.index(line) ws.cell(row=35, column=7).value = float(lines[index + 4].split()[1]) ws.cell(row=36, column=7).value = float(lines[index + 4].split()[2]) elif line.strip() == "Robot Weaving": index = lines.index(line) ws.cell(row=41, column=7).value = float(lines[index + 4].split()[2]) ws.cell(row=42, column=7).value = float(lines[index + 4].split()[3]) ws.cell(row=43, column=7).value = float(lines[index + 4].split()[4]) else: pass pdf.close() def p_iso_100(self, file, p_files, ws, tmpfile): p_files.append(file) pdf = pdfplumber.open(file) with open(tmpfile, mode="w", encoding="utf-8") as fb: for page in pdf.pages: fb.write(page.extract_text()) with open(tmpfile, mode="r", encoding="utf-8") as fb: lines = fb.readlines() lines = [line for line in lines if not line.startswith("Page ")] for line in lines: if line.strip() == "Velocity Accuracy and Repeatability": index = lines.index(line) ws.cell(row=26, column=7).value = float(lines[index + 4].split()[1]) ws.cell(row=27, column=7).value = float(lines[index + 4].split()[2]) ws.cell(row=28, column=7).value = float(lines[index + 4].split()[3]) elif line.strip()[:31] == "Path Accuracy and Repeatability": index = lines.index(line) ws.cell(row=33, column=7).value = float(lines[index + 4].split()[1]) ws.cell(row=34, column=7).value = float(lines[index + 4].split()[2]) elif line.strip() == "Corner Overshoot and Roundoff": index = lines.index(line) ws.cell(row=39, column=7).value = float(lines[index + 4].split()[1]) ws.cell(row=40, column=7).value = float(lines[index + 4].split()[2]) elif line.strip() == "Robot Weaving": index = lines.index(line) ws.cell(row=47, column=7).value = float(lines[index + 4].split()[2]) ws.cell(row=48, column=7).value = float(lines[index + 4].split()[3]) ws.cell(row=49, column=7).value = float(lines[index + 4].split()[4]) else: pass pdf.close() def p_iso_1000(self, file, p_files, ws, tmpfile): p_files.append(file) pdf = pdfplumber.open(file) with open(tmpfile, mode="w", encoding="utf-8") as fb: for page in pdf.pages: fb.write(page.extract_text()) with open(tmpfile, mode="r", encoding="utf-8") as fb: lines = fb.readlines() lines = [line for line in lines if not line.startswith("Page ")] for line in lines: if line.strip() == "Velocity Accuracy and Repeatability": index = lines.index(line) ws.cell(row=23, column=7).value = float(lines[index + 4].split()[1]) ws.cell(row=24, column=7).value = float(lines[index + 4].split()[2]) ws.cell(row=25, column=7).value = float(lines[index + 4].split()[3]) elif line.strip()[:31] == "Path Accuracy and Repeatability": index = lines.index(line) ws.cell(row=31, column=7).value = float(lines[index + 4].split()[1]) ws.cell(row=32, column=7).value = float(lines[index + 4].split()[2]) elif line.strip() == "Corner Overshoot and Roundoff": index = lines.index(line) ws.cell(row=37, column=7).value = float(lines[index + 4].split()[1]) ws.cell(row=38, column=7).value = float(lines[index + 4].split()[2]) elif line.strip() == "Robot Weaving": index = lines.index(line) ws.cell(row=44, column=7).value = float(lines[index + 4].split()[2]) ws.cell(row=45, column=7).value = float(lines[index + 4].split()[3]) ws.cell(row=46, column=7).value = float(lines[index + 4].split()[4]) else: pass pdf.close() def initialization(self): dirs, files = clibs.traversal_files(self.dir_path, self.output) if len(dirs) != 0: self.logger("ERROR", "iso", f"init: 工作目录下不可以有文件夹!", "red", "InitFileError") for file in files: file = file.lower() if file.endswith("iso-results.xlsx"): pass elif file.endswith("iso-v1000.pdf"): pass elif file.endswith("iso-v100.pdf"): pass elif file.endswith("iso.pdf"): pass else: self.logger("ERROR", "iso", f"init: 工作目录下只允许有如下四个文件,不区分大小写,pdf文件最少有一个!
1. iso-results.xlsx
2. ISO.pdf
3. ISO-V100.pdf
4. ISO-V1000.pdf", "red", "InitFileError") return files def processing(self): time_start = time.time() clibs.running[self.idx] = 1 files = self.initialization() filename = f"{self.dir_path}/iso-results.xlsx" tmpfile = f"{self.dir_path}/data.txt" wb, ws = None, None try: wb = openpyxl.load_workbook(filename) ws = wb.active for i in range(3, 50): ws.cell(row=i, column=7).value = None except Exception as Err: self.logger("ERROR", "iso", f"main: 无法打开文件 {filename}
{Err}", "red", "FileOpenError") p_files = [] for file in files: if file.split("/")[-1].lower() == "iso.pdf": self.logger("INFO", "iso", f"正在处理{file}......") self.p_iso(file, p_files, ws, tmpfile) self.logger("INFO", "iso", f"文件{file}已处理完毕。") elif file.split("/")[-1].lower() == "iso-v100.pdf": self.logger("INFO", "iso", f"正在处理{file}......") self.p_iso_100(file, p_files, ws, tmpfile) self.logger("INFO", "iso", f"文件{file}已处理完毕。") elif file.split("/")[-1].lower() == "iso-v1000.pdf": self.logger("INFO", "iso", f"正在处理{file}......") self.p_iso_1000(file, p_files, ws, tmpfile) self.logger("INFO", "iso", f"文件{file}已处理完毕。") else: pass wb.save(filename) wb.close() if len(p_files) == 0: self.logger("ERROR", "iso", f"目录 {self.dir_path} 下没有需要处理的文件,需要确认......", "red", "FileNotFound") else: os.remove(tmpfile) self.logger("INFO", "current-processing", "-" * 60 + "
全部处理完毕
", "purple") time_total = time.time() - time_start msg = f"数据处理时间:{time_total // 3600:02.0f} h {time_total % 3600 // 60:02.0f} m {time_total % 60:02.0f} s\n" self.logger("INFO", "current-processing", msg)