我有一個 excel 表,其中有幾列背景顏色。我需要獲取所有具有背景顏色的行column B。我試過styleframe但只能從 excel 中選擇特定的單元格,而不是整行。
from styleframe import StyleFrame, utils, Styler
cols = [1]
data = StyleFrame.read_excel(excel_file, read_style=True, use_openpyxl_style=false, usecols=cols)
print(data)
data.apply_style_by_index(data.index[1], Styler(bg_color='Yellow'), cols_to_style='Emp First Name')
print(data)
這是樣本excel表

我期待整行(3,4,5,7,8,10,11,12 和 13),其中有黃色背景色column B
uj5u.com熱心網友回復:
這是代碼:
import openpyxl
wb = openpyxl.load_workbook('file.xlsx', data_only=True)
ws = wb['Sheet1']
df = pd.DataFrame(columns=['Dept', 'FNAME','LNAME', '2021', '2022', '2023'])
for row in ws:
if row[1].fill.start_color.index == "FFFFFF00":
df.loc[len(df.index)] = [row[0].value, row[1].value, row[2].value, row[3].value, row[4].value, row[5].value]
僅在 B 列中的黃色單元格將被拾取,并且該行的整行(所有 6 列)將被添加到資料框中。您可以使用 dataframe 繼續執行進一步的操作df。
我的 Excel 作業表資料:

...和輸出資料框
>> df
Dept FNAME LNAME 2021 2022 2023
0 D2 User2 L2 100 200 300
1 D3 User3 L3 100 200 300
2 D4 User4 L4 100 200 300
3 D4 User8 L8 100 200 300
4 D2 User10 L10 100 200 300
5 D3 User11 L11 100 200 300
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/474711.html
