v0.0.4(2024/05/22)
1. 重新标定了get_threshold_step函数,让处理更加准确
2. 新定义了now_doing_msg函数,实时输出处理信息
3. 修改了find_row_start和find_row_start_dp函数,增加的部分相同,处理数据的时候,先判断是否是空值,或者是0,此时可以加快步进
4. 修改了just_open函数,不在做重试
This commit is contained in:
gitea 2024-05-23 11:25:45 +08:00
parent b335f61c72
commit de6d1d47c8
4 changed files with 93 additions and 75 deletions

View File

@ -1,39 +1,33 @@
# coding: utf-8
from os import scandir, remove
from sys import exit
from openpyxl import load_workbook
import os
import sys
import openpyxl
from win32com.client import DispatchEx
from time import time, strftime, localtime, sleep
import time
from threading import Thread
import pythoncom
from pandas import read_csv
import pandas
def just_open(filename):
for i in range(3):
try:
pythoncom.CoInitialize()
xlapp = DispatchEx("Excel.Application")
xlapp.Visible = False
xlbook = xlapp.Workbooks.Open(filename)
xlapp.DisplayAlerts = False
xlapp.DisplayAlerts = 0
xlbook.SaveAs(filename)
xlbook.Close()
xlapp.Quit()
except Exception as Err:
if xlbook is None:
xlbook.SaveAs(filename)
xlbook.close()
if xlapp is not None:
xlapp.Quit()
print(f"使用win32com打开【{filename}】文件,第 {i} 次操作失败,静默三秒钟,等待重新执行......")
sleep(3)
def traversal_files(path):
if not os.path.exists(path):
msg = f'数据文件夹{path}不存在,请确认后重试......'
warn_pause_exit(msg, 1, 11)
else:
dirs = []
files = []
for item in scandir(path):
for item in os.scandir(path):
if item.is_dir():
dirs.append(item.path)
elif item.is_file():
@ -65,6 +59,11 @@ def find_row_start(excel_file, ws_data, conditions, AV, RR, AXIS):
threshold, step = get_threshold_step(excel_file, AXIS)
while row_start > 0:
speed = ws_data[f"A{row_start}"].value
if speed is None or int(speed) < 1:
row_start -= step
continue
row_end = row_start - step
if row_end < 2:
msg = f"可能是{excel_file.replace('xlsx', 'data')}, 这个文件数据采集有问题,也有可能是程序步长设定问题......" \
@ -78,7 +77,7 @@ def find_row_start(excel_file, ws_data, conditions, AV, RR, AXIS):
else:
row_start -= step
else:
remove(excel_file)
os.remove(excel_file)
msg = f"可能是{excel_file.replace('xlsx', 'data')},这个文件数据采集有问题,比如采集的时机不对,请检查......"
warn_pause_exit(msg, 1, 9)
@ -136,8 +135,9 @@ def copy_data_to_excel_file(wb_data, ws_result, row_max, row_start, excel_file,
ws_dp.cell(row=6, column=7).value = RR
wb_data.save(excel_file)
wb_data.close()
just_open(excel_file) # 为了能读取到公式计算的数值,必须要用 win32com 打开关闭一次
wb_data = load_workbook(excel_file, data_only=True)
wb_data = openpyxl.load_workbook(excel_file, data_only=True)
ws_dp = wb_data['dp']
return wb_data, ws_dp
@ -149,9 +149,10 @@ def find_row_start_dp(data_file, ws_dp, row_max, row_start, conditions, AV):
row_max_dp = row_max - row_start + 1 + 1 # title row
row_start_dp = row_max_dp - 5
while row_start_dp > 6:
# 处理异常数据当从数据文件中拷贝的有效数据超过5000时会触发该代码块
if ws_dp.cell(row=row_start_dp, column=4).value is None:
row_start_dp -= 100
# 处理异常数据当从数据文件中拷贝的有效数据超过5000时会触发下面代码块
angular = ws_dp.cell(row=row_start_dp, column=4).value
if angular is None or str(angular) == '0':
row_start_dp -= 50
continue
_a = float(ws_dp.cell(row=row_start_dp, column=4).value)
_b = float(ws_dp.cell(row=row_start_dp - 1, column=4).value)
@ -172,9 +173,9 @@ def find_row_start_dp(data_file, ws_dp, row_max, row_start, conditions, AV):
def single_file_process(data_file, wb_result, count, AV, RR, RC, AXIS):
excel_file = data_file.replace('data', 'xlsx')
excel_file = data_file.replace('.data', '.xlsx')
sheet_name = data_file.split('\\')[-1].removesuffix('.data')
df = read_csv(data_file, sep='\t')
df = pandas.read_csv(data_file, sep='\t')
df.to_excel(excel_file, sheet_name=sheet_name, index=False)
conditions = sorted(data_file.split('\\')[-2].split('_')[1:])
@ -182,7 +183,7 @@ def single_file_process(data_file, wb_result, count, AV, RR, RC, AXIS):
result_sheet_name = find_result_sheet_name(conditions, count)
ws_result = wb_result[result_sheet_name]
wb_data = load_workbook(excel_file)
wb_data = openpyxl.load_workbook(excel_file)
ws_data = wb_data[sheet_name]
row_max, row_start = find_row_start(excel_file, ws_data, conditions, AV, RR, AXIS)
@ -196,25 +197,46 @@ def single_file_process(data_file, wb_result, count, AV, RR, RC, AXIS):
wb_data.close()
def now_doing_msg(docs, flag):
now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
file_type = 'file' if os.path.isfile(docs) else 'dir'
if flag == 'start' and file_type == 'dir':
print(f"[{now}] 正在处理目录【{docs}】中的数据......")
elif flag == 'start' and file_type == 'file':
print(f"[{now}] 正在处理文件【{docs}】中的数据......")
elif flag == 'done' and file_type == 'dir':
print(f"[{now}] 目录【{docs}】数据文件已处理完毕......")
elif flag == 'done' and file_type == 'file':
print(f"[{now}] 文件【{docs}】数据文件已处理完毕......")
def data_process(result_file, raw_data_dirs, AV, RR, RC, AXIS):
prefix = result_file.split('\\')[-1].split('_')[0]
wb_result = load_workbook(result_file) # 打开和关闭结果文件夹十分耗时间
wb_result = openpyxl.load_workbook(result_file) # 打开和关闭结果文件夹十分耗时间
for raw_data_dir in raw_data_dirs:
if raw_data_dir.split('\\')[-1].split('_')[0] == prefix:
now = strftime('%Y-%m-%d %H:%M:%S', localtime(time()))
print(f"[{now}] 正在处理目录【{raw_data_dir}】中的数据......")
now_doing_msg(raw_data_dir, 'start')
_, data_files = traversal_files(raw_data_dir)
# 数据文件串行处理模式---------------------------------
# count = 1
# for data_file in data_files:
# now_doing_msg(data_file, 'start')
# single_file_process(data_file, wb_result, count, AV, RR, RC, AXIS)
# count += 1
# now_doing_msg(data_file, 'done')
# ---------------------------------------------------
# 数据文件并行处理模式---------------------------------
threads = [Thread(target=single_file_process, args=(data_files[0], wb_result, 1, AV, RR, RC, AXIS)),
Thread(target=single_file_process, args=(data_files[1], wb_result, 2, AV, RR, RC, AXIS)),
Thread(target=single_file_process, args=(data_files[2], wb_result, 3, AV, RR, RC, AXIS))]
[t.start() for t in threads]
[t.join() for t in threads]
now = strftime('%Y-%m-%d %H:%M:%S', localtime(time()))
print(f"[{now}] 目录【{raw_data_dir}】中的数据已处理完成......")
now_doing_msg(raw_data_dir, 'done')
# ---------------------------------------------------
now = strftime('%Y-%m-%d %H:%M:%S', localtime(time()))
print(f"[{now}] 结果文件【{result_file}】的数据已整理完成,保存文件需要1-2min请耐心等待......")
now_doing_msg(result_file, 'done')
print(f"保存文件需要1-2min请耐心等待......")
wb_result.save(result_file)
wb_result.close()
@ -223,7 +245,7 @@ def warn_pause_exit(msg, pause_num, exit_num):
print(msg + '\n')
for i in range(pause_num):
_ = input("Press ENTER to continue......\n")
exit(exit_num)
sys.exit(exit_num)
def check_files(raw_data_dirs, result_files):
@ -262,7 +284,7 @@ def check_files(raw_data_dirs, result_files):
_, raw_data_files = traversal_files(raw_data_dir)
for raw_data_file in raw_data_files:
if raw_data_file.endswith(".xlsx"):
remove(raw_data_file)
os.remove(raw_data_file)
_, raw_data_files = traversal_files(raw_data_dir)
if len(raw_data_files) != 3:
@ -281,35 +303,32 @@ def delete_excel_files(raw_data_dirs):
_, raw_data_files = traversal_files(raw_data_dir)
for raw_data_file in raw_data_files:
if raw_data_file.endswith('.xlsx'):
remove(raw_data_file)
os.remove(raw_data_file)
def initialization():
time_start = time() # 记录开始时间
time_start = time.time() # 记录开始时间
try:
# read init configurations from config file
wb_conf = load_workbook('./configuration.xlsx', read_only=True)
ws_conf = wb_conf['conf']
wb_conf = openpyxl.load_workbook('./configs.xlsx', read_only=True)
ws_conf = wb_conf['brake']
data_dir = ws_conf.cell(row=2, column=2).value
DATA_DIR = ws_conf.cell(row=2, column=2).value
AV = int(ws_conf.cell(row=3, column=2).value)
RR = int(ws_conf.cell(row=4, column=2).value)
RC = float(ws_conf.cell(row=5, column=2).value)
AXIS = ws_conf.cell(row=6, column=2).value
wb_conf.close()
except Exception as Err:
msg = "无法在当前路径下找到或打开【configs.xlsx】文件请确认"
warn_pause_exit(msg, 1, 2)
raw_data_dirs, result_files = traversal_files(data_dir)
# print("#调试信息======================================")
# print(f"结果文件:{result_files}")
# print(f'数据目录:{raw_data_dirs}')
raw_data_dirs, result_files = traversal_files(DATA_DIR)
delete_excel_files(raw_data_dirs)
check_files(raw_data_dirs, result_files)
return raw_data_dirs, result_files, time_start, AV, RR, RC, AXIS
except Exception as Err:
msg = "无法在当前路径下找到【configuration.xlsx】文件请确认"
warn_pause_exit(msg, 1, 2)
def execution(args):
raw_data_dirs, result_files, time_start, AV, RR, RC, AXIS = args
@ -318,17 +337,16 @@ def execution(args):
prefix.append(raw_data_dir.split('\\')[-1].split("_")[0])
try:
threads = []
# threads = []
for result_file in result_files:
if result_file.split('\\')[-1].split('_')[0] not in set(prefix):
continue
else:
now = strftime('%Y-%m-%d %H:%M:%S', localtime(time()))
print(f"[{now}] 正在整理结果文件【{result_file}】的数据......")
# data_process(result_file, raw_data_dirs, AV, RR, RC, AXIS)
threads.append(Thread(target=data_process, args=(result_file, raw_data_dirs, AV, RR, RC, AXIS)))
[t.start() for t in threads]
[t.join() for t in threads]
now_doing_msg(result_file, 'start')
data_process(result_file, raw_data_dirs, AV, RR, RC, AXIS)
# threads.append(Thread(target=data_process, args=(result_file, raw_data_dirs, AV, RR, RC, AXIS)))
# [t.start() for t in threads]
# [t.join() for t in threads]
print("#---------------------------------------------------------")
print("全部处理完毕")
delete_excel_files(raw_data_dirs)
@ -336,7 +354,7 @@ def execution(args):
print("程序运行错误,请检查配置文件是否准确设定,以及数据文件组织是否正确!")
delete_excel_files(raw_data_dirs) # 运行结束之后,删除中间临时文件
time_end = time() # 记录结束时间
time_end = time.time() # 记录结束时间
time_total = time_end - time_start # 计算的时间差为程序的执行时间,单位为秒/s
msg = f"数据处理时间:{time_total//3600:02} h {time_total % 3600/60:05.2f} min"
warn_pause_exit(msg, 1, 0)

BIN
rokae/brake/configs.xlsx Normal file

Binary file not shown.

Binary file not shown.

View File

@ -6,8 +6,8 @@ VSVersionInfo(
ffi=FixedFileInfo(
# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
# Set not needed items to zero 0.
filevers=(0, 0, 3, 0),
prodvers=(0, 0, 3, 0),
filevers=(0, 0, 4, 0),
prodvers=(0, 0, 4, 0),
# Contains a bitmask that specifies the valid bits 'flags'r
mask=0x3f,
# Contains a bitmask that specifies the Boolean attributes of the file.
@ -31,12 +31,12 @@ VSVersionInfo(
'040904b0',
[StringStruct('CompanyName', 'Rokae - https://www.rokae.com/'),
StringStruct('FileDescription', 'All in one automatic operating tool'),
StringStruct('FileVersion', '0.0.3 (2024-05-20)'),
StringStruct('FileVersion', '0.0.4 (2024-05-20)'),
StringStruct('InternalName', 'AIO.exe'),
StringStruct('LegalCopyright', '© 2024-2024 Manford Fan'),
StringStruct('OriginalFilename', 'AIO.exe'),
StringStruct('ProductName', 'AIO'),
StringStruct('ProductVersion', '0.0.3 (2024-05-20)')])
StringStruct('ProductVersion', '0.0.4 (2024-05-20)')])
]),
VarFileInfo([VarStruct('Translation', [1033, 1200])])
]