gpt4 book ai didi

python - set_column 未将颜色格式应用于大型 Excel 文件的列

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:10 25 4
gpt4 key购买 nike

我试图使用不同的字体颜色突出显示列,以便于用户通知,但它没有发生。尝试了各种方法但不确定问题是什么。除了其他几次尝试之外,我还尝试评论其他工作表

with pd.ExcelWriter("C:\myDocs\Python\MAY_CCPS_CCSO_Preload_Validated.xlsx", engine='xlsxwriter') as writer:
CCSO_Preload_Sheet1.to_excel(writer, index = False, sheet_name='SO_1Header')
# CCSO_Preload_Sheet2.to_excel(writer, index = False, sheet_name="SO_2Item",engine='xlsxwriter')
# CCSO_Preload_Sheet3.to_excel(writer, index = False, sheet_name="SO_3_Schedules",engine='xlsxwriter')
# CCSO_Preload_Sheet4.to_excel(writer, index = False, sheet_name="SO_4Partners",engine='xlsxwriter')
# CCSO_Preload_Sheet5.to_excel(writer, index = False, sheet_name="SO_5Long_Text",engine='xlsxwriter')
workbook = writer.book
worksheet = writer.sheets['SO_1Header']
format1 = workbook.add_format({'bold': True, 'font_color': 'red', 'bg_color': 'yellow'})
worksheet.set_column('B:B', 40, format1)
writer.save()

最佳答案

看起来问题是您正在使用 with 上下文管理器,但格式化代码位于该 block 之外。因此,当您尝试格式化该文件时,该文件已经关闭。

更改缩进程序可以按预期工作:

import pandas as pd

data = [10, 20, 30, 40, 50, 60]

CCSO_Preload_Sheet1 = pd.DataFrame({'Heading1': data,
'Heading2': data})

with pd.ExcelWriter("test.xlsx", engine='xlsxwriter') as writer:
CCSO_Preload_Sheet1.to_excel(writer,
index = False,
sheet_name='SO_1Header')

workbook = writer.book
worksheet = writer.sheets['SO_1Header']

format1 = workbook.add_format({'bold': True,
'font_color': 'red',
'bg_color': 'yellow'})

worksheet.set_column('B:B', 40, format1)

输出:

enter image description here

关于python - set_column 未将颜色格式应用于大型 Excel 文件的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55594245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com