我有一个在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