import xlwings as xw
import time
start_row = 10
end_row = 59
start_open_time = time.time()
print('读取 Excel 文件')
app = xw.App(visible=False, add_book=False)
excel_path = 'C:/Users/15526414163/Desktop/pyfile/数据建模v3.xlsx'
wb = app.books.open(excel_path)
sheetslen = len(wb.sheets)
sql_str = ''
for sheetspag in range(1, sheetslen - 2):
sheet = wb.sheets[sheetspag]
sheet_str = str(sheetspag)
tableEName = sheet.range('B3').value
tableCName = sheet.range('B2').value
sql_str = sql_str + 'CREATE TABLE IF NOT EXISTS ' + 'PDATA.' + tableEName + f" (\n"
for row in range(10, 200):
row_str = str(row)
rowvalue = sheet.range('A' + row_str).value
if rowvalue == None:
break
else:
value1 = sheet.range('C' + row_str).value
value2 = sheet.range('E' + row_str).value
value3 = sheet.range('D' + row_str).value
if value1 != None or value2 != None:
if row > 10:
sql_str = sql_str + ' ,' + value1 + ' ' + value2 + ' COMMENT ' + f"'{value3}'\n"
else:
sql_str = sql_str + ' ' + value1 + ' ' + value2 + ' COMMENT ' + f"'{value3}'\n"
sql_str = sql_str + f") COMMENT " + f"'{tableCName}'\n"
sql_str = sql_str + 'STORED AS PARQUET;\n'
sql_str = sql_str + '------------------------------------------------------\n'
file_name = 'create_table_sql_' + time.strftime("%Y%m%d%H%M%S", time.localtime()) + '.txt'
output = 'C:/Users/15526414163/Desktop/pyfile/' + file_name
with open(output, mode='a', encoding='utf-8') as f:
f.write(sql_str)
print(file_name + "写入完成")
wb.save()
print("保存 Excel 文件完成")
app.quit()
app.kill()
print("Excel 文件已退出")