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.
57 lines
1.7 KiB
57 lines
1.7 KiB
import os
|
|
import openpyxl
|
|
|
|
# 文件路径
|
|
output_file = r'D:\计量经济学\计量实验资料及作业要求\计量实验资料及作业要求\UGC回归数据.xlsx'
|
|
|
|
print("========================================")
|
|
print(" 创建UGC回归数据文件")
|
|
print("========================================")
|
|
print(f"输出文件: {output_file}")
|
|
print()
|
|
|
|
# 检查输出目录是否存在
|
|
output_dir = os.path.dirname(output_file)
|
|
print(f"输出目录: {output_dir}")
|
|
print(f"目录存在: {os.path.exists(output_dir)}")
|
|
|
|
if not os.path.exists(output_dir):
|
|
print("正在创建输出目录...")
|
|
try:
|
|
os.makedirs(output_dir)
|
|
print("目录创建成功")
|
|
except Exception as e:
|
|
print(f"创建目录失败: {e}")
|
|
exit(1)
|
|
|
|
# 创建新的Excel文件
|
|
try:
|
|
print("\n创建新的Excel文件...")
|
|
wb = openpyxl.Workbook()
|
|
ws = wb.active
|
|
|
|
# 设置第一行列名
|
|
headers = ['Y', 'X1', 'X2', 'X3', 'X4', 'X5', 'X6']
|
|
for i, header in enumerate(headers, 1):
|
|
ws.cell(row=1, column=i, value=header)
|
|
|
|
# 保存文件
|
|
print(f"保存文件到: {output_file}")
|
|
wb.save(output_file)
|
|
|
|
# 验证文件是否创建成功
|
|
if os.path.exists(output_file):
|
|
print(f"文件已成功创建: {output_file}")
|
|
print(f"文件大小: {os.path.getsize(output_file) / 1024:.2f} KB")
|
|
else:
|
|
print("错误: 文件创建失败")
|
|
|
|
print()
|
|
print("========================================")
|
|
print(" 任务完成")
|
|
print("========================================")
|
|
|
|
except Exception as e:
|
|
print(f"处理文件时出错: {str(e)}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|