Python 2.7 如何使用report lab创建可以动态更改行数的表
我正在创建一个PDF,其中包含一个表,但不同PDF的表行不同Python 2.7 如何使用report lab创建可以动态更改行数的表,python-2.7,reportlab,Python 2.7,Reportlab,我正在创建一个PDF,其中包含一个表,但不同PDF的表行不同 table = Table(data, colWidths=[3.05 * cm, 4.5 * cm, 4 * cm,3* cm, 3 * cm]) table.setStyle(TableStyle([ ('INNERGRID', (0,0), (-1,-1), 0.25, black), ('BOX', (0,0), (-1,-1), 0.
table = Table(data, colWidths=[3.05 * cm, 4.5 * cm, 4 * cm,3* cm, 3 * cm])
table.setStyle(TableStyle([
('INNERGRID', (0,0), (-1,-1), 0.25, black),
('BOX', (0,0), (-1,-1), 0.25, black),
]))
table.wrapOn(self.pdf, self.width_table, self.height_table)
table.drawOn(self.pdf, *self.coord(1.0, 18.6,cm))
这就是我现在用来创建表的方法
谢谢,这是每个刚开始工作的人的中心问题 下面是开始构建的完整可行代码。答案是,正如你在评论中写的那样,是循环。不过,以后的人会发现的问题是如何处理多页表以及这些表的设计。有各种各样的解决方案值得研究
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.platypus import Frame, PageTemplate, KeepInFrame
from reportlab.lib.units import cm
from reportlab.platypus import (Table, TableStyle, BaseDocTemplate)
########################################################################
def create_pdf():
"""
Create a pdf
"""
# Create a frame
text_frame = Frame(
x1=3.00 * cm, # From left
y1=1.5 * cm, # From bottom
height=19.60 * cm,
width=15.90 * cm,
leftPadding=0 * cm,
bottomPadding=0 * cm,
rightPadding=0 * cm,
topPadding=0 * cm,
showBoundary=1,
id='text_frame')
# Create a table
test_table = []
data = []
for i in range(11, 1, -1):
column1data = f'Column_1 on row {i}'
column2data = f'Column_2 on row {i}'
data.append([column1data, column2data])
data_table = Table(data, 15.90 * cm / 2)
data_table.setStyle(TableStyle([
# Title
('LEFTPADDING', (0, 0), (1, 0), 0),
('RIGHTPADDING', (0, 0), (1, 0), 0),
('VALIGN', (0, 0), (1, 0), 'TOP'),
('ALIGN', (0, 0), (1, 0), 'CENTRE'),
# DataTable
('ALIGN', (0, 1), (1, -1), 'CENTRE'),
('SIZE', (0, 1), (-1, -1), 7),
('LEADING', (0, 1), (-1, -1), 8.4),
('VALIGN', (0, 1), (-1, -1), 'MIDDLE'),
('TOPPADDING', (0, 1), (-1, -1), 2.6),
('BOTTOMPADDING', (0, 1), (-1, -1), 2.6),
('LINEBELOW', (0, 1), (-1, -1), 0.3, colors.gray),
]))
test_table.append(data_table)
test_table = KeepInFrame(0, 0, test_table, mode='overflow')
# Building the story
story = [test_table] # adding test_table table (alternative, story.add(test_table))
# Establish a document
doc = BaseDocTemplate("Example_output.pdf", pagesize=letter)
# Creating a page template
frontpage = PageTemplate(id='FrontPage',
frames=[text_frame]
)
# Adding the story to the template and template to the document
doc.addPageTemplates(frontpage)
# Building doc
doc.build(story)
# ----------------------------------------------------------------------
if __name__ == "__main__":
create_pdf() # Printing the pdf
我不明白。你的问题是什么?我在reportlab中寻找一个函数,该函数将在给定行参数的情况下创建一个表,但我只使用了一个循环,这很有效。