read_excel_fields.py
1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
读取Excel文件,提取所有字段信息
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
try:
import openpyxl
excel_path = 'excel/工资全字段.xlsx'
if not os.path.exists(excel_path):
excel_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), 'excel/工资全字段.xlsx')
wb = openpyxl.load_workbook(excel_path, data_only=True)
ws = wb.active
print(f'Sheet名称: {ws.title}')
print(f'总行数: {ws.max_row}')
print(f'总列数: {ws.max_column}')
print('\n' + '='*80)
print('表头(所有字段):')
print('='*80)
headers = []
for i, cell in enumerate(ws[1], 1):
header_value = cell.value if cell.value else f'Column{i}'
headers.append(header_value)
print(f'{i:3d}. {header_value}')
print('\n' + '='*80)
print('前3行数据示例:')
print('='*80)
for row_idx in range(2, min(5, ws.max_row + 1)):
print(f'\n--- 第 {row_idx} 行 ---')
for col_idx, header in enumerate(headers, 1):
cell_value = ws.cell(row=row_idx, column=col_idx).value
if cell_value is not None:
# 只显示有值的字段
cell_str = str(cell_value)
if len(cell_str) > 50:
cell_str = cell_str[:50] + '...'
print(f' {header}: {cell_str}')
print('\n' + '='*80)
print(f'所有字段列表(共 {len(headers)} 个):')
print('='*80)
print(', '.join(headers))
except ImportError:
print('错误: 需要安装 openpyxl 库')
print('请运行: pip install openpyxl')
sys.exit(1)
except Exception as e:
print(f'错误: {e}')
import traceback
traceback.print_exc()
sys.exit(1)