如何在位置10,100处放置python Reportlab表并使用drawString [英] How to position a python Reportlab table at position 10,100 and use drawString

查看:673
本文介绍了如何在位置10,100处放置python Reportlab表并使用drawString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a python hobbyist and reportlab newbie.
I know how to use drawString to put text at a particular place on a page, e.g.: c.drawString(10,100,"Welcome to Reportlab!")

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

But I can't figure out how to place a table (which will only be a few lines long) so that the table begins at the same position as the c.drawString(10,100,"Welcome to Reportlab!"), which I will place elsewhere if I learn how to put my table there instead.

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

I also can't figure out how to use drawString in the same script, since using a canvas is the only way I know how to use the drawString functioning. My 4 lines of canvas code (following this paragraph) would close the canvas/file and create the PDF. The table code (further below) also closes the file and builds the PDF, and I don't see how to use the "doc.build(elements)" line to close the canvas which I use for drawString operations.

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()

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

I would appreciate any guidance you could give me (1) about how to place the table so that it begins at 10,100, and (2) how to use drawString in the same script. If some of my code does nothing useful, please don't assume I put it there intentionally; I tried to copy enough from examples, so that my table would have wordwrap functioning.

这是我一直在玩的代码:

Here's the code I have been playing with:

# 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)

推荐答案

最近,我偶然发现了同一问题.这里的问题是,在reportlab中,表是所谓的"flowables",而drawString命令是固定的".

Lately, I stumbled across the same issue. The problem here is that in reportlab, tables are so-called "flowables" whereas the drawString command is "fixed".

感谢Mike Driscoll撰写的出色教程,我找到了解决方案:.

I found a solution thanks to this great tutorial written by Mike Driscoll: "Reportlab: Mixing Fixed Content and Flowables".

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

Here is a slighty adapted version which constitutes a working snippet:

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()

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

I can also recommend two more tutorials by Mike Driscoll, which have allowed me to get quickly familiar with reportlab.

  • A Simple Step-by-Step Reportlab Tutorial
  • Reportlab Tables – Creating Tables in PDFs with Python

非常感谢,迈克!

这篇关于如何在位置10,100处放置python Reportlab表并使用drawString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆