我有一个xlsx文件,其中列具有各种颜色。
我只想使用pandas在python中读取此excel的白色列,但是我不了解这样做的任何线索。
我能够将完整的excel读入数据框,但是随后我错过了有关列着色的信息,而且我不知道要删除哪些列以及不删除哪些列。
最佳答案
(公开:我是我将建议的图书馆的作者之一)
使用StyleFrame(包裹 Pandas ),您可以将excel文件读入数据帧,而不会丢失样式数据。
考虑以下工作表:
和以下代码:from styleframe import StyleFrame, utils
# from StyleFrame import StyleFrame, utils (if using version < 3.X)
sf = StyleFrame.read_excel('test.xlsx', read_style=True)
print(sf)
# b p y
# 0 nan 3 1000.0
# 1 3.0 4 2.0
# 2 4.0 5 42902.72396767148
sf = sf[[col for col in sf.columns
if col.style.fill.fgColor.rgb in ('FFFFFFFF', utils.colors.white)]]
# "white" can be represented as 'FFFFFFFF' or
# '00FFFFFF' (which is what utils.colors.white is set to)
print(sf)
# b
# 0 nan
# 1 3.0
# 2 4.0