`
import reportlab.platypus
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import mm, inch
from reportlab.platypus import Table, TableStyle
#function below is a constructor to get values for the parameters
# I have tried to position each elements manually
def page_builder(my_canvas, assitemno, assitemdesc, ready, shipment, table1record, noteorder, comment):
width, height = letter
my_canvas.drawString(40, 800, 'Assembly Item Number')
my_canvas.drawString(40, 780, assitemno)
my_canvas.drawString(360, 800, 'Assembly Item Description')
my_canvas.drawString(360, 780, assitemdesc)
my_canvas.drawString(40, 750, 'Ready')
my_canvas.drawString(40, 730, ready)
my_canvas.drawString(360, 750, 'Shipment')
my_canvas.drawString(360, 730, shipment)
my_canvas.drawString(40, 700, 'Notes')
my_canvas.drawString(40, 670, noteorder)
my_canvas.drawString(40, 640, 'Comments')
my_canvas.drawString(40, 610, comment)
datatable = buildtabledata(table1record)
tblstyle = reportlab.platypus.TableStyle(
[('INNERGRID', (0, 0), (-1, -1), 0.25, colors.darkslateblue), ('VALIGN', (0, 0), (-1, -1), 'TOP'),
('BOX', (0, 0), (-1, -1), 0.25, colors.black),
('GRID', (0, 0), (-1, -1), 0.01 * inch, (0, 0, 0,)), ])
tblstyle1 = reportlab.platypus.TableStyle(
[('FONT', (0, 0), (-1, -1), 'Courier', 12), ('TEXTCOLOR', (0, 0), (-1, 0), colors.darkgreen),
('FONT', (0, 0), (-1, 0), 'Courier', 14)])
datatable.setStyle(tblstyle)
datatable.setStyle(tblstyle1)
# please ignore the table style as I have tried to use 2 styles together, it somehow works
datatable.wrapOn(my_canvas, 400, 250)
# trying to wrap the table at designated position
datatable.drawOn(my_canvas, 40, 420)
如果行数少于10,则表位置完美,但是如果它超过10或更多,则表将向上拉伸,并与drawString元素重叠。如果行数超过页面的限制,我希望表大部分向下向下移动。
my_canvas.saveState()
my_canvas.showPage()
my_canvas.save()
def buildtabledata(table1record):
data = [['{}'.format(x) for x in ['Description', 'Number', 'Qty']]]
for record in table1record:
recordtoappend = [record[1], record[2], record[3]]
data.append(recordtoappend)
return Table(data, repeatRows=1)`
非常感谢任何帮助。很抱歉输入密码。我真的是python和reportlab的新手。原谅我的无知。