如何在位置10,100处放置python Reportlab表并使用drawString

Mar*_*kin 1 reportlab drawstring python-2.7

我是python爱好者和reportlab新手。
我知道如何使用drawString将文本放在页面上的特定位置,例如:c.drawString(10,100,“ Welcome to Reportlab!”)

但是我不知道如何放置表格(只有几行长),以便表格从与c.drawString(10,100,“ Welcome to Reportlab!”)相同的位置开始如果我学习如何将桌子放在那儿,可以放在其他地方。

我也无法弄清楚如何在同一脚本中使用drawString,因为使用画布是我知道如何使用drawString函数的唯一方法。我的4行画布代码(在本段之后)将关闭画布/文件并创建PDF。该表代码(下面进一步)也关闭了文件并构建了PDF,并且我看不到如何使用“ doc.build(elements)”行来关闭用于drawString操作的画布。

c = canvas.Canvas(r"e:\hellonu.pdf", pagesize=letter)
c.setFont("Courier", 9) #choose your font type and font size
c.drawString(10,60,"Welcome to Reportlab!")
c.save()
Run Code Online (Sandbox Code Playgroud)

我将不胜感激您可以为我提供的所有指导(1)如何放置表格,使其始于10,100,以及(2)如何在同一脚本中使用drawString。如果我的某些代码无用,请不要以为我是故意放进去的;我尝试从示例中复制足够的内容,以便我的表具有自动换行功能。

这是我一直在玩的代码:

# http://zewaren.net/site/node/139
from reportlab.lib import colors
from reportlab.lib.pagesizes import LETTER, inch, portrait
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
from reportlab.lib.styles import getSampleStyleSheet


doc = SimpleDocTemplate(r"e:\test_report_lab.pdf", pagesize=LETTER, rightMargin=30,leftMargin=30, topMargin=30,bottomMargin=18)
doc.pagesize = portrait(LETTER)
elements = []


data = [
["Directory"],
["AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA "],
]


style = TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'),
                       ('TEXTCOLOR',(1,1),(-2,-2),colors.red),
                       ('VALIGN',(0,0),(0,-1),'TOP'),
                       ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                       ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                       ])

#Configure style and word wrap
s = getSampleStyleSheet()
s = s["BodyText"]
s.wordWrap = 'CJK'
data2 = [[Paragraph(cell, s) for cell in row] for row in data]
t=Table(data2)
t.setStyle(style)


#Send the data and build the file
elements.append(t)
doc.build(elements)
Run Code Online (Sandbox Code Playgroud)

nos*_*mus 6

最近,我偶然发现了同一问题。这里的问题是,在reportlab中,表是所谓的“ flowables”,而drawString命令是“ fixed”的。

感谢Mike Driscoll撰写的这篇出色的教程,找到了一个解决方案:“ Reportlab:混合固定内容和Flowables”

以下是经过改编的版本,构成了有效的代码段:

from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
from reportlab.platypus import Image, Paragraph, Table
from reportlab.lib import colors

c = canvas.Canvas('example.pdf', pagesize=A4)  # alternatively use bottomup=False
width, height = A4

data = [[1, 2, 3], [2, 1, 3], [3, 2, 1]]

table = Table(data, colWidths=10*mm)
table.setStyle([("VALIGN", (0,0), (-1,-1), "MIDDLE"),
                ("ALIGN", (0,0), (-1,-1), "CENTER"),
                ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black)])

table.wrapOn(c, width, height)
table.drawOn(c, 0*mm, 5*mm)

styles = getSampleStyleSheet()    
ptext = "This is an example."
p = Paragraph(ptext, style=styles["Normal"])
p.wrapOn(c, 50*mm, 50*mm)  # size of 'textbox' for linebreaks etc.
p.drawOn(c, 0*mm, 0*mm)    # position of text / where to draw

c.save()
Run Code Online (Sandbox Code Playgroud)

我还可以推荐Mike Driscoll撰写的另外两个教程,这些教程使我快速熟悉了reportlab。

非常感谢,迈克!