如何将文本字符串添加到 Reportlab PDF table 文档?
How do I add strings of text to a Reportlab PDF table document?
下面是我使用 Reportlab 库创建的 table,特别是来自 reportlab.platypus 的 SimpleDocTemplate:
# Data for this example
data = [
['Animal', 'Name', 'Color'],
['Dog', 'Fido', 'Brown'],
['Cat', 'Mittens', 'Black'],
['Fish', 'Bubbles', 'Orange']
]
fileName = 'pdfTable.pdf'
# Using a template to make the PDF
from reportlab.platypus import SimpleDocTemplate # With this, our table will automatically be centered in the document
from reportlab.lib.pagesizes import letter
pdf = SimpleDocTemplate (
fileName,
pagesize=letter
)
# Import table functionality and create table
from reportlab.platypus import Table
table = Table(data)
# Add style
from reportlab.platypus import TableStyle
from reportlab.lib import colors
style = TableStyle([
('BACKGROUND', (0,0), (3,0), colors.green),
('TEXTCOLOR', (0,0), (-1,0), colors.whitesmoke), # The negative one means "go to the last element"
('ALIGN', (0,0), (-1,-1), 'CENTER'),
('FONTNAME', (0,0), (-1,0), 'Courier-Bold'),
('FONTSIZE', (0,0), (-1,0), 14),
('BOTTOMPADDING', (0,0), (-1,0), 12), # 12 = 12 pixels
('BACKGROUND', (0,1), (-1,-1), colors.beige), # Background for the rest of the table (excluding the title row)
])
table.setStyle(style)
elems = []
elems.append(table)
pdf.build(elems)
在 PDF 中生成以下 table:
我想在文档的其他地方添加文本字符串,在 table 之外。通常,我会使用 reportlab.pdfgen:
中的“canvas”
from reportlab.pdfgen import canvas
pdf = canvas.Canvas('myFile.pdf')
pdf.drawCentredString(300, 770, 'Title')
但这不适用于 SimpleDocTemplate,我相信它只是创建了一个新文档。如何结合 table?
创建独立的文本字符串
reportlab.platypus.SimpleDocTemplate
从可流动对象创建文档。 Flowable 是一个内容块,占据一些垂直 space。如果你想在 table 之前添加标题,只需创建带有标题样式的段落并将其放在 [=17= 之前的列表中],如果你想在 [=17= 之后添加文本,请将其放在后面。如果您想要任意放置的文本,请创建一个函数并将其作为 onFirstPage
参数传递给 SimpleDocTemplate.build
。
from reportlab.platypus import Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import cm
styles = getSampleStyleSheet()
flowables = [
Paragraph('Title', styles['Title']),
table,
Spacer(1 * cm, 1 * cm),
Paragraph('text after spacer')
]
def onFirstPage(canvas, document):
canvas.drawCentredString(100, 100, 'Text drawn with onFirstPage')
pdf.build(flowables, onFirstPage=onFirstPage)
下面是我使用 Reportlab 库创建的 table,特别是来自 reportlab.platypus 的 SimpleDocTemplate:
# Data for this example
data = [
['Animal', 'Name', 'Color'],
['Dog', 'Fido', 'Brown'],
['Cat', 'Mittens', 'Black'],
['Fish', 'Bubbles', 'Orange']
]
fileName = 'pdfTable.pdf'
# Using a template to make the PDF
from reportlab.platypus import SimpleDocTemplate # With this, our table will automatically be centered in the document
from reportlab.lib.pagesizes import letter
pdf = SimpleDocTemplate (
fileName,
pagesize=letter
)
# Import table functionality and create table
from reportlab.platypus import Table
table = Table(data)
# Add style
from reportlab.platypus import TableStyle
from reportlab.lib import colors
style = TableStyle([
('BACKGROUND', (0,0), (3,0), colors.green),
('TEXTCOLOR', (0,0), (-1,0), colors.whitesmoke), # The negative one means "go to the last element"
('ALIGN', (0,0), (-1,-1), 'CENTER'),
('FONTNAME', (0,0), (-1,0), 'Courier-Bold'),
('FONTSIZE', (0,0), (-1,0), 14),
('BOTTOMPADDING', (0,0), (-1,0), 12), # 12 = 12 pixels
('BACKGROUND', (0,1), (-1,-1), colors.beige), # Background for the rest of the table (excluding the title row)
])
table.setStyle(style)
elems = []
elems.append(table)
pdf.build(elems)
在 PDF 中生成以下 table:
我想在文档的其他地方添加文本字符串,在 table 之外。通常,我会使用 reportlab.pdfgen:
中的“canvas”from reportlab.pdfgen import canvas
pdf = canvas.Canvas('myFile.pdf')
pdf.drawCentredString(300, 770, 'Title')
但这不适用于 SimpleDocTemplate,我相信它只是创建了一个新文档。如何结合 table?
创建独立的文本字符串reportlab.platypus.SimpleDocTemplate
从可流动对象创建文档。 Flowable 是一个内容块,占据一些垂直 space。如果你想在 table 之前添加标题,只需创建带有标题样式的段落并将其放在 [=17= 之前的列表中],如果你想在 [=17= 之后添加文本,请将其放在后面。如果您想要任意放置的文本,请创建一个函数并将其作为 onFirstPage
参数传递给 SimpleDocTemplate.build
。
from reportlab.platypus import Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import cm
styles = getSampleStyleSheet()
flowables = [
Paragraph('Title', styles['Title']),
table,
Spacer(1 * cm, 1 * cm),
Paragraph('text after spacer')
]
def onFirstPage(canvas, document):
canvas.drawCentredString(100, 100, 'Text drawn with onFirstPage')
pdf.build(flowables, onFirstPage=onFirstPage)