ERP/test_py/validate_excel.py
2025-11-25 10:35:02 +08:00

60 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
验证Excel文件格式是否符合MAC与批次导入要求
"""
import sys
import pandas as pd
import warnings
warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl')
def validate_excel(file_path):
"""
验证Excel文件格式
返回: (is_valid, error_message)
"""
try:
df = pd.read_excel(file_path)
if df.empty:
return False, "文件为空,没有数据"
columns = df.columns.tolist()
# 检查是否有批次号列
if '批次号' not in columns:
return False, "缺少必需的列:批次号"
# 检查是否有MAC或SN_MAC列
has_mac = 'MAC' in columns
has_sn_mac = 'SN_MAC' in columns
if not has_mac and not has_sn_mac:
return False, "缺少必需的列MAC 或 SN_MAC"
# 检查列数应该只有2列
if len(columns) != 2:
return False, f"文件应该只包含2列数据当前有{len(columns)}列:{', '.join(columns)}"
# 验证通过
mac_col = 'MAC' if has_mac else 'SN_MAC'
return True, f"文件格式正确,包含列:{mac_col} 和 批次号,共{len(df)}行数据"
except Exception as e:
return False, f"读取文件失败:{str(e)}"
if __name__ == '__main__':
if len(sys.argv) < 2:
print("用法: python validate_excel.py <excel文件路径>")
sys.exit(1)
file_path = sys.argv[1]
is_valid, message = validate_excel(file_path)
if is_valid:
print(f"{message}")
sys.exit(0)
else:
print(f"{message}")
sys.exit(1)