如何使用条件 python 为(背景)excel 单元格着色并将相同颜色的 sheet 写回 excel?

How to color(background) excel cell using conditions using python and write the same colored sheet back to excel?

嗨,我有 excel sheet 名为 'master'

Start       Customer    Country Parent group                Issue
2           Jaguar      USA     TATA                        HW
3           Suzuki      Japan   Suzuki Motors               HW
6           GM          USA     General Motors              SW
7           Hyundai     Japan   Hyundai Motrs               HW
8           Google      USA     Google corporations         SW
12          TCS         India   TATA Consultancy Services   HW
14          Jaguar      USA     TATA                        HW
19          AT & T      USA     AT & T Corporation          SW
28          Oracle      USA     Oracle Services             SW
30          Benz        Germany Mercedez Benz               SW
31          Suzuki      Japan   Suzuki Motors               SW
49          Honda       Japan   HONDA motors                HW
90          GM          USA     General Motors              SW
91          Hyundai     Japan   Hyundai motors              HW
96          Jaguar      USA     TATA                        SW
98          Suzuki      Japan   Suzuki Motors               HW
100         TCS         India   TATA Consultancy Services   HW
114         Hyundai     Japan   Hyundai motors              HW
119         AT & T      USA     AT & T Corporation          SW
125         Suzuki      Japan   Suzuki Motors               SW

我想使用 for 循环遍历这四个客户的列表,为客户为本田、TCS、捷豹和铃木的客户列的单元格着色。 列表 = ['Honda'、'TCS'、'Jaguar'、'Suzuki']

并在excelsheet

中写入彩色数据框

你用 StyleFrame 标记了这个问题,所以我想你想要一个使用它的解决方案。

from StyleFrame import StyleFrame, Styler

sf = StyleFrame.read_excel('path/to/excel.xlsx', sheetname='master')

yellow_bg = Styler(bg_color='yellow')

sf.apply_style_by_indexes(sf[sf['Customer'].isin(('Honda', 'TCS', 'Jaguar', 'Suzuki'))],
                          styler_obj=yellow_bg,
                          cols_to_style='Customer')

sf.to_excel('path/to/excel.xlsx').save()

如果您不想使用 Excel 的默认 sheet 名称('Sheet1')