You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.6 KiB
54 lines
1.6 KiB
import os
|
|
import openpyxl
|
|
|
|
# 文件路径
|
|
input_file = r'D:\计量经济学\计量实验资料及作业要求\计量实验资料及作业要求\图文帖子原始信息计量实验使用.xlsx'
|
|
|
|
print("========================================")
|
|
print(" 读取Excel测试")
|
|
print("========================================")
|
|
print(f"输入文件: {input_file}")
|
|
print()
|
|
|
|
# 检查文件是否存在
|
|
if not os.path.exists(input_file):
|
|
print("错误: 输入文件不存在!")
|
|
exit(1)
|
|
|
|
print(f"文件大小: {os.path.getsize(input_file) / 1024:.2f} KB")
|
|
|
|
# 读取Excel文件
|
|
try:
|
|
print("正在读取Excel文件...")
|
|
wb = openpyxl.load_workbook(input_file)
|
|
ws = wb.active
|
|
|
|
print(f"工作表名称: {ws.title}")
|
|
print(f"最大行数: {ws.max_row}")
|
|
print(f"最大列数: {ws.max_column}")
|
|
|
|
# 读取表头
|
|
print("\n表头:")
|
|
headers = []
|
|
for col in range(1, ws.max_column + 1):
|
|
header = ws.cell(row=1, column=col).value
|
|
headers.append(header)
|
|
print(f"{col}. {header}")
|
|
|
|
# 读取前3行数据
|
|
print("\n前3行数据:")
|
|
for row in range(2, min(5, ws.max_row + 1)):
|
|
row_data = []
|
|
for col in range(1, min(10, ws.max_column + 1)):
|
|
value = ws.cell(row=row, column=col).value
|
|
row_data.append(value)
|
|
print(f"行 {row}: {row_data}")
|
|
|
|
print("\n========================================")
|
|
print(" 读取完成")
|
|
print("========================================")
|
|
|
|
except Exception as e:
|
|
print(f"处理文件时出错: {str(e)}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|