我有一個在openpyxl中創建的工作簿,並且正在嘗試使用查詢中的df填充工作表。但是,當我打開xlsx時,工作表已創建,但所有查詢都連接到第一個工作表,其他工作表都是空白的。哪裏不對?OPENPYXL:寫入新的工作表
下面是代碼:
from openpyxl import Workbook
# Create the hospital_ranking workbook
hospital_ranking = Workbook()
dest_filename1 = "hospital_ranking.xlsx"
ws1 = hospital_ranking.active
ws1.title = "Nationwide"
from openpyxl.utils.dataframe import dataframe_to_rows
# Write the nationwide query to ws1
for r in dataframe_to_rows(national_results, index = False, header = True):
ws1.append(r)
for cell in ws1['A'] + ws1[1]:
cell.style = 'Pandas'
hospital_ranking.save(filename = staging_dir + dest_filename1)
# Create the worksheet for each focus state
# CA
ws2 = hospital_ranking.create_sheet(title = 'California')
ws2 = hospital_ranking.active
# Write the CA query to ws2
for r in dataframe_to_rows(ca_results, index = False, header = True):
ws2.append(r)
for cell in ws2['A'] + ws2[1]:
cell.style = 'Pandas'
hospital_ranking.save(filename = staging_dir + dest_filename1)
*電子表格中的信息錯誤* ...出了什麼問題?列混合了嗎?行刪除?完全不同的數據?缺失數據?你的代碼看起來是創建空狀態命名錶,但沒有數據。 – Parfait
你不想索引,但你故意將它們設置爲「真」。也許你應該在複製和粘貼代碼時多閱讀一下代碼。 –
@Parfait - 電子表格中填充了電子表格中完全不同的數據,電子表格加載到程序的更高版本 – zsad512