Skip to content

Commit

Permalink
Cli发布
Browse files Browse the repository at this point in the history
  • Loading branch information
Littleor committed Aug 10, 2021
1 parent d82adde commit f797b5c
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 15 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# TDMS-Conver

[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)

- [背景](#背景)
- [安装](#安装)
- [示例](#示例)
- [更多](更多 )
- [维护者](#维护者)
- [如何贡献](#如何贡献)
- [使用许可](#使用许可)

## 背景

在使用NI的相关传感器采集数据时,一般的保存格式为`.tdms`,然而这种格式可能不方便用Python直接处理和阅读, 故推出了**TDMS-Conver**来将`.tdms`格式文件转换为`.xlsx`格式的Excel文件便于读写。

## 安装

这个项目基于Python3。请确保你本地安装的Python版本大于3。

```sh
$ pip install TDMS-Conver
$ # pip3 install TDMS-Conver # 如果使用的是pip3
```

## 示例

**TDMS-Conver**可以实现**单个文件**的转换和**文件夹**的转换。

```bash
# 文件夹
tdms-conver ./data-dir
# 文件
tdms-conver ./data-dir/data.tdms
# 指定保存目录
tdms-conver ./data-dir -s ./output
```

## 更多


## 维护者

[@Littleor](https://github.com/Littleor)

## 如何贡献

非常欢迎你的加入![提一个 Issue](https://github.com/Littleor/TDMS-Conver/issues/new) 或者提交一个 Pull Request。


标准 Readme 遵循 [Contributor Covenant](http://contributor-covenant.org/version/1/3/0/) 行为规范。


## 使用许可

[MIT](LICENSE) © Littleor
Empty file added conver/__init__.py
Empty file.
58 changes: 43 additions & 15 deletions main.py → conver/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
from nptdms import TdmsFile
import os, sys, shutil
import argparse
import os
import shutil
import sys

import pandas as pd
from nptdms import TdmsFile

APP_DESC = """
这是一个简单好用的tdms格式转换为xlsx的工具.
"""
# print(APP_DESC)

if len(sys.argv) == 1:
sys.argv.append('--help')
parser = argparse.ArgumentParser()
# parser.add_argument('-v', '--verbose', default=0, help="print more debuging information")
parser.add_argument('-s', '--store', help="保存输出的文件到指定位置")
parser.add_argument('path', metavar='Path', nargs='+', help="tdms文件/文件夹路径")
args = parser.parse_args()

source_dir: str = str(args.path[0]).strip()

if os.path.isdir(source_dir):
output_path = source_dir + '/output'
else:
output_path = source_dir

if args.store is not None:
output_path = str(args.store).strip()

print('请输入文件路径')
source_dir: str = sys.stdin.readline().strip()

def get_metadata_info(tdms_file_path: str):
# 这里主要是避免不清楚 group_name和channel_name导致无法提取数据,建议前期先跑这个函数来输出对应的信息
Expand All @@ -14,7 +39,8 @@ def get_metadata_info(tdms_file_path: str):
channel_name = channel.name
properties = channel.properties
data = channel[:]
print('group_name', group_name,'channel_name', channel_name,'len', len(data))
print('group_name', group_name, 'channel_name', channel_name, 'len', len(data))


def conver_to_excel(tdms_file_path: str, output_path: str):
tdms_file = TdmsFile.read(tdms_file_path)
Expand All @@ -28,13 +54,14 @@ def conver_to_excel(tdms_file_path: str, output_path: str):
if len(data) > 0:
if excel_data is None:
excel_data = pd.DataFrame(data, columns=[f'{group_name}-{channel_name}'])
#else:
# else:
# excel_data[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)

def cover_dir_to_excel(dir_path: str, output_dir_path:str):

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'):
Expand All @@ -44,29 +71,30 @@ def cover_dir_to_excel(dir_path: str, output_dir_path:str):
print(f'{file_name} 转换完成')



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):
file_path = os.path.join(search_path + '/' + file_name)
if os.path.isdir(file_path):
if file_name[0] != '.':
dfs(file_path)

if __name__ == '__main__':


def main():
if not os.path.exists(source_dir):
print('目标路径不存在')

if os.path.isdir(source_dir):
output_path = source_dir + '/output'

if not os.path.exists(output_path):
os.makedirs(output_path)
else:
shutil.rmtree(output_path)
os.makedirs(output_path)

dfs(source_dir)
else:
conver_to_excel(source_dir, source_dir.replace('.tdms', '.xlsx'))




if __name__ == '__main__':
main()
File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="TDMS-Conver",
version="0.0.1",
author="Littleor",
license='MIT',
author_email="[email protected]",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
install_requires=[
'pandas>=1.1.5',
'openpyxl>=3.0.7',
'npTDMS>=1.3.0'
],
url="https://github.com/Littleor/TDMS-Conver.git",
packages=setuptools.find_packages(),
classifiers=[
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'License :: OSI Approved :: MIT License',
],
entry_points={
'console_scripts': [
'tdms-conver=conver.main:main',
]
}
)

0 comments on commit f797b5c

Please sign in to comment.