Python FPDF 基于 IF 语句填充单元格颜色。

huangapple go评论67阅读模式
英文:

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):

Python FPDF 基于 IF 语句填充单元格颜色。

在[Jupyter]中等效的方式是使用Styler

(
    df.style
     .applymap(lambda x: "background-color: #C1E5FC"
               if x == "No" else "", subset=["Column B"])
)

Python FPDF 基于 IF 语句填充单元格颜色。

更新:

> 实际上我有四列。在列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")

输出:

Python FPDF 基于 IF 语句填充单元格颜色。

使用的输入:

  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[&quot;Column B&quot;].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(&quot;Arial&quot;, &quot;B&quot;, 12) # &lt;-- ajudst here if needed
for col in df.columns:
    pdf.cell(30, 10, col, 1, 0, &quot;C&quot;, 1)
pdf.ln(10) #a newline

for _, row in df.iterrows():
    pdf.set_font(&quot;Arial&quot;, &quot;&quot;, 10)  # &lt;-- ajudst here if needed
    pdf.cell(30, 10, row[&quot;Column A&quot;], 1, 0, &quot;C&quot;)

    if row[&quot;Column B&quot;] == &quot;No&quot;:
        pdf.set_fill_color(193, 229, 252) #make a blue-bg
        pdf.cell(30, 10, row[&quot;Column B&quot;], 1, 0, &quot;C&quot;, 1)
    else:
        pdf.cell(30, 10, row[&quot;Column B&quot;], 1, 0, &quot;C&quot;)

    pdf.ln(10) #a newline

pdf.output(&quot;output.pdf&quot;, &quot;F&quot;)

Output (.pdf) :

Python FPDF 基于 IF 语句填充单元格颜色。

Equivalent with Styler (in Jupyter) :

(
    df.style
     .applymap(lambda x: &quot;background-color: #C1E5FC&quot;
               if x == &quot;No&quot; else &quot;&quot;, subset=[&quot;Column B&quot;])
)

Python FPDF 基于 IF 语句填充单元格颜色。

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(&quot;Arial&quot;, &quot;&quot;, 10)
    pdf.cell(30, 10, row[&quot;Column 1&quot;], 1, 0, &quot;C&quot;) # &lt;-- add this line
    pdf.cell(30, 10, row[&quot;Column 2&quot;], 1, 0, &quot;C&quot;) # &lt;-- add this line
    pdf.cell(30, 10, row[&quot;Column A&quot;], 1, 0, &quot;C&quot;)

    if row[&quot;Column B&quot;] == &quot;No&quot;:
        pdf.set_fill_color(193, 229, 252) #make a blue-bg
        pdf.cell(30, 10, row[&quot;Column B&quot;], 1, 0, &quot;C&quot;, 1)
    else:
        pdf.cell(30, 10, row[&quot;Column B&quot;], 1, 0, &quot;C&quot;)

    pdf.ln(10) #a newline

pdf.output(&quot;output.pdf&quot;, &quot;F&quot;)

Output :

Python FPDF 基于 IF 语句填充单元格颜色。

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

huangapple
  • 本文由 发表于 2023年5月11日 19:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定