From 27f99af26f7a70ca13760ba134709f34e27729d6 Mon Sep 17 00:00:00 2001 From: Littleor Date: Tue, 23 Nov 2021 20:15:43 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=BC=95=E5=85=A5=E8=BF=9B=E5=BA=A6?= =?UTF-8?q?=E6=9D=A1=202.=20=E4=BF=AE=E6=AD=A3=E5=A4=A7=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=AF=BC=E8=87=B4Excel=E8=B6=85=E5=87=BA=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E9=97=AE=E9=A2=98=203.=20=E6=B7=BB=E5=8A=A0=E5=A4=9A=E5=88=97?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=94=AF=E6=8C=81=204.=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=BB=86=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conver/main.py | 40 +++++++++++++++++++++++++++++----------- setup.py | 5 +++-- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/conver/main.py b/conver/main.py index b1e1162..0b70183 100644 --- a/conver/main.py +++ b/conver/main.py @@ -1,10 +1,12 @@ import argparse import os +import platform import shutil import sys import pandas as pd from nptdms import TdmsFile +from tqdm import trange APP_DESC = """ 这是一个简单好用的tdms格式转换为xlsx的工具. @@ -30,6 +32,7 @@ output_path = str(args.store).strip() +# 获取信息: 用于debug def get_metadata_info(tdms_file_path: str): # 这里主要是避免不清楚 group_name和channel_name导致无法提取数据,建议前期先跑这个函数来输出对应的信息 tdms_file = TdmsFile.read(tdms_file_path) @@ -42,7 +45,10 @@ def get_metadata_info(tdms_file_path: str): print('group_name', group_name, 'channel_name', channel_name, 'len', len(data)) +# 将对应tdms文件转换为excel def conver_to_excel(tdms_file_path: str, output_path: str): + file_name = tdms_file_path[tdms_file_path.rindex('/') + 1:] + # for group in tqdm(tdms_file.groups(), desc=file_name, ascii=platform.system().lower() == 'windows'): tdms_file = TdmsFile.read(tdms_file_path) excel_data = None for group in tdms_file.groups(): @@ -50,27 +56,39 @@ def conver_to_excel(tdms_file_path: str, output_path: str): for channel in group.channels(): channel_name = channel.name properties = channel.properties - data = channel[:] + data = pd.Series(channel[:]) + if len(data) > 1048576: + print("Warn: 数据过长超出Excel限制长度1048576, 已裁剪") + data = data[:1048576 - 1] if len(data) > 0: if excel_data is None: excel_data = pd.DataFrame(data, columns=[f'{group_name}-{channel_name}']) - # else: - # excel_data[f'{group_name}-{channel_name}'] = data + else: + excel_data.insert(excel_data.shape[1], f'{group_name}-{channel_name}', data) if not os.path.exists(output_path[:output_path.rfind('/')]): os.makedirs(output_path[:output_path.rfind('/')]) excel_data.to_excel(output_path, index=None) +# 将目录下所有tdms文件转换为excel def cover_dir_to_excel(dir_path: str, output_dir_path: str): print(f'开始转换文件夹: {dir_path}') - for file_name in os.listdir(dir_path): - if file_name.endswith('.tdms'): - tdms_path = os.path.join(dir_path + '/' + file_name) - output_path = output_dir_path + '/' + file_name.replace('.tdms', '.xlsx') - conver_to_excel(tdms_path, output_path) - print(f'{file_name} 转换完成') - - + path_file_list = [] + for name in os.listdir(dir_path): + if os.path.splitext(name)[1] == '.tdms': + path_file_list.append(name) + current_file_name = path_file_list[0] + process_bar = trange(len(path_file_list), desc=current_file_name, ascii=platform.system().lower() == 'windows') + for i in process_bar: + current_file_name = path_file_list[i] + tdms_path = os.path.join(dir_path + '/' + current_file_name) + output_path = output_dir_path + '/' + current_file_name.replace('.tdms', '.xlsx') + conver_to_excel(tdms_path, output_path) + if i < len(path_file_list) - 1: + process_bar.set_description(path_file_list[i + 1]) + + +# DFS算法用于遍历内部文件夹 def dfs(search_path: str): cover_dir_to_excel(search_path, output_path + search_path.replace(source_dir, '')) for file_name in os.listdir(search_path): diff --git a/setup.py b/setup.py index d2e7a53..6eac8b2 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="TDMS-Conver", - version="0.0.1", + version="0.0.3", author="Littleor", license='MIT', author_email="me@littleor.cn", @@ -15,7 +15,8 @@ install_requires=[ 'pandas>=1.1.5', 'openpyxl>=3.0.7', - 'npTDMS>=1.3.0' + 'npTDMS>=1.3.0', + 'tqdm>=4.62.3', ], url="https://github.com/Littleor/TDMS-Conver.git", packages=setuptools.find_packages(),