Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

python - Adding a background color to Cell OpenPyXL

I'm trying to make it where my code gives off a Green background on cell when it is "Present" and a Red background on cell if it is "Absent". Here is my code.

ws1.cell(column=1, row=t, value="%s" % blue_student_list)
if (student_check(i)):
    ws1.cell(column=2, row=t, value="%s" % "Present")
else:
    ws1.cell(column=2, row=t, value="%s" % "Absent")

This code works flawlessly, I'm just wondering how I can add in the background color behind the cell.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With openpyxl 2.5.3, the code above does not work.

after trying, following code worked:

from openpyxl.styles import PatternFill
sheet['A1'].fill = PatternFill(start_color="FFC7CE", end_color="FFC7CE", fill_type = "solid")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...