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

47 lines
1.3 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
import pandas as pd
import openpyxl
import warnings
# 过滤openpyxl的跨平台兼容性警告
warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl')
file_path = '/home/hyx/work/batch_import_xlsx/sn_test_tx.xlsx'
print("检查Excel文件信息...")
try:
# 使用openpyxl检查工作表兼容Windows到Mac的Excel文件
wb = openpyxl.load_workbook(file_path, data_only=True)
print(f"工作表数量: {len(wb.sheetnames)}")
print(f"工作表名称: {wb.sheetnames}")
if wb.sheetnames:
ws = wb.active
print(f"活动工作表: {ws.title}")
print(f"最大行数: {ws.max_row}")
print(f"最大列数: {ws.max_column}")
# 显示前几行数据
print("\n前10行数据:")
for i, row in enumerate(ws.iter_rows(values_only=True), 1):
if i <= 10:
print(f"{i}行: {row}")
else:
break
wb.close() # 关闭工作簿释放资源
except Exception as e:
print(f"openpyxl错误: {e}")
print("提示: 这可能是Windows到Mac的Excel文件兼容性问题")
try:
# 使用pandas检查
print("\n使用pandas检查...")
xl_file = pd.ExcelFile(file_path)
print(f"pandas检测到的工作表: {xl_file.sheet_names}")
except Exception as e:
print(f"pandas错误: {e}")