[英]Conditional formatting under xlsxwriter enginer
I have six different values in dataframe column 'index_val' that I have imported to a each new worksheets for the same workbook.我在数据框列“index_val”中有六个不同的值,我已将它们导入到同一工作簿的每个新工作表中。
df_dict = {}
for zone in df['index_val'].unique():
zone_df = df[df['index_val'] == zone]
df_dict[zone] = zone_df
def save_xlsx(df_dict, path):
with pd.ExcelWriter(path) as writer:
for key in df_dict:
df_dict[key].to_excel(writer, key, index=False)
writer.save()
save_xlsx(df_dict, 'report.xlsx')
The output produced in Excel sheet hasn't been formatted to fit the column width. Excel 工作表中生成的输出尚未格式化以适合列宽。 Say, values in column 1 require a column width of 92, and values in column B are of width 16. I tried using the set_column
method, but since the sheet names are stored as a dictionary, it's throwing back an error.比如说,第 1 列中的值要求列宽为 92,而 B 列中的值的宽度为 16。我尝试使用set_column
方法,但由于工作表名称存储为字典,因此会抛出错误。
A chuck of that data is like this:该数据的一个夹子是这样的:
name_char index_val count
name1 A 36
name2 A 38
name3 B 76
name4 C 12
name3 C 25
name6 F 42
There will be 6 sheets, each having names from index_val.将有 6 张纸,每张纸的名称都来自 index_val。 Each sheet will have first column name_char, and second column as unique index_val.每个工作表都有第一列 name_char,第二列作为唯一的 index_val。 What do I like to have is to set the first column width to 92, and second column to 16. Or, is there a process to set the width automatically for each columns based on their maximum column value?我喜欢将第一列的宽度设置为 92,将第二列的宽度设置为 16。或者,是否有一个过程可以根据每列的最大列值自动设置每列的宽度?
You can do the following:您可以执行以下操作:
df_dict = {}
for zone in df['index_val'].unique():
zone_df = df[df['index_val'] == zone]
df_dict[zone] = zone_df
def save_xlsx(df_dict, path):
with pd.ExcelWriter(path) as writer:
for key in df_dict:
df_dict[key].to_excel(writer, key, index=False)
workbook = writer.book
worksheet = writer.sheets[key]
worksheet.set_column(1, 1, 92)
worksheet.set_column(2, 2, 16)
writer.save()
save_xlsx(df_dict, 'report.xlsx')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.