Skip to content

Commit

Permalink
1. 引入进度条
Browse files Browse the repository at this point in the history
2. 修正大文件导致Excel超出报错问题
3. 添加多列数据支持
4. 优化细节
  • Loading branch information
Littleor committed Nov 23, 2021
1 parent 14483e6 commit 27f99af
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
40 changes: 29 additions & 11 deletions conver/main.py
Original file line number Diff line number Diff line change
@@ -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的工具.
Expand All @@ -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)
Expand All @@ -42,35 +45,50 @@ 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():
group_name = group.name
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):
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="TDMS-Conver",
version="0.0.1",
version="0.0.3",
author="Littleor",
license='MIT',
author_email="[email protected]",
Expand All @@ -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(),
Expand Down

0 comments on commit 27f99af

Please sign in to comment.