'cell()'或"multi_cell()"中的粗体和/或斜体文本?



如何在同一个单元格中使用不同的字体!

的例子:在这个场景中,您将看到一条行驶路线

我想要的:

在这个场景中,您将看到一个驾驶路线

我发现这个GIT问题有同样的问题。

有一个解决方法:

from fpdf import FPDF
DATA = (
("First name", "Last name", "Age", "City"),
("Lucas", "Cimon", "31", "Saint-        -sur-Loire"),
)
COL_WIDTHS = (.2, .2, .15, .45)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Courier", size=10)         # this trick is easier with monospaced fonts
line_height = pdf.font_size * 2.5
for row in DATA:
for i, datum in enumerate(row):
col_width = COL_WIDTHS[i] * pdf.epw
x, y = pdf.x, pdf.y
pdf.multi_cell(col_width, line_height, datum, border=1, ln=3)
if datum.endswith("sur-Loire"):  # performing a 2nd pass on the target cell
text = "      Mathurin"      # part of cell context to put in bold, with padding matching the word horizontal position
pdf.set_xy(x, y)             # positioning FPDF to re-draw the same cell
pdf.set_font(style="BI")     # switching to bold italic
pdf.multi_cell(col_width, line_height, text, border=1, ln=3)
pdf.set_font(style="")       # switching back to regular
pdf.ln(line_height)
pdf.output('issue_108.pdf')

最新更新