Это в значительной степени способ достичь этого. Это не очень , так как StyleFrame не был предназначен для такого использования.
Чтение исходного файла Excel
import numpy as np
from StyleFrame import StyleFrame, utils
sf = StyleFrame.read_excel('test.xlsx', read_style=True, use_openpyxl_styles=False)
1) Все ячейки, кроме ячеек с красным фоном, пусты
def empty_red_background_cells(cell):
if cell.style.bg_color in {utils.colors.red, 'FFFF0000'}:
cell.value = np.nan
return cell
sf_1 = StyleFrame(sf.applymap(empty_red_background_cells))
print(sf_1)
# C1 C2 C3 C4 C5 C6
# 0 a1 1.0 s nan 1001.0 1234.0
# 1 a2 12.0 s nan 1001.0 4322.0
# 2 a3 nan s nan 1001.0 4432.0
# 3 a4 232.0 s nan 1001.0 4432.0
# 4 a5 343.0 s 99.0 nan nan
# 5 a6 3.0 s 99.0 1001.0 4432.0
# 6 a7 34.0 s 99.0 1001.0 4432.0
# 7 a8 5.0 s nan 1001.0 4432.0
# 8 a9 6.0 s 99.0 1001.0 4432.0
# 9 a10 565.0 s 99.0 nan 4432.0
# 10 a11 5543.0 s 99.0 1001.0 4432.0
# 11 a12 112.0 s 99.0 1001.0 nan
# 12 a13 34345.0 s 99.0 1001.0 4432.0
# 13 a14 0.0 s 99.0 nan nan
# 14 a15 453.0 s 99.0 1001.0 nan
2) Только ячейки с красным текстом
def only_cells_with_red_text(cell):
return cell if cell.style.font_color in {utils.colors.red, 'FFFF0000'} else np.nan
sf_2 = StyleFrame(sf.applymap(only_cells_with_red_text).dropna(axis=(0, 1), how='all'))
# passing a tuple to pandas.dropna is deprecated since pandas 0.23.0, but this can be
# avoided by simply calling dropna twice, once with axis=0 and once with axis=1
print(sf_2)
# C2 C6
# 7 nan 4432.0
# 8 nan 4432.0
# 9 565.0 nan
# 10 5543.0 nan
# 11 112.0 nan
3) Только ячейки с красным фоном
def only_cells_with_red_background(cell):
return cell if cell.style.bg_color in {utils.colors.red, 'FFFF0000'} else np.nan
sf_3 = StyleFrame(sf.applymap(only_cells_with_red_background).dropna(axis=(0, 1), how='all'))
# passing a tuple to pandas.dropna is deprecated since pandas 0.23.0, but this can be
# avoided by simply calling dropna twice, once with axis=0 and once with axis=1
print(sf_3)
# C4 C6
# 0 99.0 nan
# 1 99.0 nan
# 2 99.0 nan
# 3 99.0 nan
# 13 nan 4432.0
# 14 nan 4432.0