英文:
Python FPDF fill color of a cell based on an IF statement
问题
如果要根据条件改变表格的单元格背景颜色,你可以尝试以下代码:
# 导入必要的库
from fpdf import FPDF
# 创建PDF对象
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
# 假设你有一个DataFrame df,你可以遍历每一行,检查 Column B 的值
# 如果值是 "No",则设置单元格背景颜色为指定颜色
for index, row in df.iterrows():
if row["Column B"] == "No":
pdf.set_fill_color(193, 229, 252) # 设置背景颜色
else:
pdf.set_fill_color(255, 255, 255) # 设置默认背景颜色
pdf.cell(30, 10, row["Column B"], 1, 1, 'C', True) # 创建单元格
# 输出PDF文件
pdf.output("output.pdf")
这个代码会遍历DataFrame中的每一行,如果"Column B"的值是"No",就会将单元格的背景颜色设为指定的颜色,否则将其设为默认颜色。最后,将生成的PDF保存为"output.pdf"。
英文:
Team,
How do I change the cell background color of a table. For example, I want to change the cell background of column B items if it is "No" using FPDF and IF statement or other methods. That is if a No is in the cell, the "No" cell should be colored.
Column A | Column B |
---|---|
Yes | No |
Yes | No |
if df.Column B.any() == "No":
pdf.set_fill_color(193, 229, 252)
pdf.cell(30, 10, 'No', 1, 1, 'C', True)
But it did not work.
答案1
得分: 0
Here is the translated content:
除了df.Column B
中的语法错误外,df["Column B"].any()
始终返回True
。
您可以尝试使用iterrows
从[tag:pandas]:
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_fill_color(255, 255, 255) # 设置白色背景
pdf.set_font("Arial", "B", 12) # <--- 如有需要,请进行调整
for col in df.columns:
pdf.cell(30, 10, col, 1, 0, "C", 1)
pdf.ln(10) # 换行
for _, row in df.iterrows():
pdf.set_font("Arial", "", 10) # <--- 如有需要,请进行调整
pdf.cell(30, 10, row["Column A"], 1, 0, "C")
if row["Column B"] == "No":
pdf.set_fill_color(193, 229, 252) # 设置蓝色背景
pdf.cell(30, 10, row["Column B"], 1, 0, "C", 1)
else:
pdf.cell(30, 10, row["Column B"], 1, 0, "C")
pdf.ln(10) # 换行
pdf.output("output.pdf", "F")
输出 (.pdf):
在[Jupyter]中等效的方式是使用Styler
:
(
df.style
.applymap(lambda x: "background-color: #C1E5FC"
if x == "No" else "", subset=["Column B"])
)
更新:
> 实际上我有四列。在列A之前有2列。如何为每个在列B之前的列重写pdf.cell(30, 10, row["Column A"], 1)
?
for _, row in df.iterrows():
pdf.set_font("Arial", "", 10)
pdf.cell(30, 10, row["Column 1"], 1, 0, "C") # <-- 添加这一行
pdf.cell(30, 10, row["Column 2"], 1, 0, "C") # <-- 添加这一行
pdf.cell(30, 10, row["Column A"], 1, 0, "C")
if row["Column B"] == "No":
pdf.set_fill_color(193, 229, 252) # 设置蓝色背景
pdf.cell(30, 10, row["Column B"], 1, 0, "C", 1)
else:
pdf.cell(30, 10, row["Column B"], 1, 0, "C")
pdf.ln(10) # 换行
pdf.output("output.pdf", "F")
输出:
使用的输入:
Column 1 Column 2 Column A Column B
0 A X foo Yes
1 B Y bar No
2 C Z baz Yes
英文:
Besides the syntax error in df.Column B
, df["Column B"].any()
will always returns True
.
You can try using iterrows
from [tag:pandas] :
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_fill_color(255, 255, 255) #make a white-bg
pdf.set_font("Arial", "B", 12) # <-- ajudst here if needed
for col in df.columns:
pdf.cell(30, 10, col, 1, 0, "C", 1)
pdf.ln(10) #a newline
for _, row in df.iterrows():
pdf.set_font("Arial", "", 10) # <-- ajudst here if needed
pdf.cell(30, 10, row["Column A"], 1, 0, "C")
if row["Column B"] == "No":
pdf.set_fill_color(193, 229, 252) #make a blue-bg
pdf.cell(30, 10, row["Column B"], 1, 0, "C", 1)
else:
pdf.cell(30, 10, row["Column B"], 1, 0, "C")
pdf.ln(10) #a newline
pdf.output("output.pdf", "F")
Output (.pdf) :
Equivalent with Styler
(in Jupyter) :
(
df.style
.applymap(lambda x: "background-color: #C1E5FC"
if x == "No" else "", subset=["Column B"])
)
UPDATE :
> Actually I have four columns. There are 2 columns before column A. How
> do I re-write for pdf.cell(30, 10, row["Column A"], 1) for each column
> preceeding column B ?
for _, row in df.iterrows():
pdf.set_font("Arial", "", 10)
pdf.cell(30, 10, row["Column 1"], 1, 0, "C") # <-- add this line
pdf.cell(30, 10, row["Column 2"], 1, 0, "C") # <-- add this line
pdf.cell(30, 10, row["Column A"], 1, 0, "C")
if row["Column B"] == "No":
pdf.set_fill_color(193, 229, 252) #make a blue-bg
pdf.cell(30, 10, row["Column B"], 1, 0, "C", 1)
else:
pdf.cell(30, 10, row["Column B"], 1, 0, "C")
pdf.ln(10) #a newline
pdf.output("output.pdf", "F")
Output :
Input used :
Column 1 Column 2 Column A Column B
0 A X foo Yes
1 B Y bar No
2 C Z baz Yes
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论