Python 在表格报告实验室中换行文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4726011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:12:54  来源:igfitidea点击:

Wrap text in a table reportlab?

pythonreportlab

提问by Alquimista

I use a table but, I draw in in a canvas to control the position of the flowables, this because I have a template in a pdf, an I merge with pyPDF.

我使用表格,但是,我在画布中绘制以控制流动对象的位置,这是因为我在 pdf 中有一个模板,我与 pyPDF 合并。

The wrap is done in a table but the text go up, not down that's what I hope.

包装是在一张桌子上完成的,但文本向上,而不是向下,这就是我希望的。

c is the canvas

c 是画布

Code

代码

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

width, height = A4
styles = getSampleStyleSheet()

def coord(x, y, unit=1):
    x, y = x * unit, height -  y * unit
    return x, y

descrpcion = Paragraph('long paragraph', styles["Normal"])
partida = Paragraph('1', styles["Center"])
candidad = Paragraph('120', styles["Center"])
precio_unitario = Paragraph('.00', styles["right"])
precio_total = Paragraph('40.00', styles["right"])

data= [[partida, candidad, descrpcion, precio_unitario, precio_total]]
table = Table(data, colWidths=[2.05 * cm, 2.7 * cm, 9.6 * cm,
                               2.65 * cm, 2.7 * cm])

c = canvas.Canvas(PDF, pagesize=A4)
table.wrapOn(c, width, height)
table.drawOn(c, *coord(1.8, 9.6, cm))
c.save()

http://img600.imageshack.us/img600/3203/reportld.jpg

http://img600.imageshack.us/img600/3203/reportld.jpg

采纳答案by Nicholas TJ

The description text went up as you wrap it in a styles["Normal"] You can try to wrap your text in a styles["BodyText"] This will allow your text to align themselves according to the width of the cell you specify. You could also include formatting which is similar to HTML text formatting.

当您将描述文本包裹在样式 ["Normal"] 中时,您可以尝试将文本包裹在样式 ["BodyText"] 中,这将允许您的文本根据您指定的单元格的宽度自行对齐。您还可以包含类似于 HTML 文本格式的格式。

Then use TableStyle to format the content in the table, for example, color text, center paragraph, span rows/columns and so on.

然后使用 TableStyle 对表格中的内容进行格式化,例如,彩色文本、居中段落、跨行/列等。

I edited the code above to a working version (example):

我将上面的代码编辑为工作版本(示例):

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, Table, TableStyle
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
from reportlab.lib import colors

width, height = A4
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
styleN.alignment = TA_LEFT
styleBH = styles["Normal"]
styleBH.alignment = TA_CENTER

def coord(x, y, unit=1):
    x, y = x * unit, height -  y * unit
    return x, y

# Headers
hdescrpcion = Paragraph('''<b>descrpcion</b>''', styleBH)
hpartida = Paragraph('''<b>partida</b>''', styleBH)
hcandidad = Paragraph('''<b>candidad</b>''', styleBH)
hprecio_unitario = Paragraph('''<b>precio_unitario</b>''', styleBH)
hprecio_total = Paragraph('''<b>precio_total</b>''', styleBH)

# Texts
descrpcion = Paragraph('long paragraph', styleN)
partida = Paragraph('1', styleN)
candidad = Paragraph('120', styleN)
precio_unitario = Paragraph('.00', styleN)
precio_total = Paragraph('40.00', styleN)

data= [[hdescrpcion, hcandidad,hcandidad, hprecio_unitario, hprecio_total],
       [partida, candidad, descrpcion, precio_unitario, precio_total]]

table = Table(data, colWidths=[2.05 * cm, 2.7 * cm, 5 * cm,
                               3* cm, 3 * cm])

table.setStyle(TableStyle([
                       ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                       ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                       ]))

c = canvas.Canvas("a.pdf", pagesize=A4)
table.wrapOn(c, width, height)
table.drawOn(c, *coord(1.8, 9.6, cm))
c.save()

回答by Woooee

I know that Postscript's reference is the lower, left corner. I'm guessing that PDF is the same, so you subtract from the y value to go down. Print the beginning and ending "y" values in the function to see how they are changing and adjust the "y" value depending on the length of the sentence. And how does the function know what "height" is? I use ReportLab but could probably help with a specific example if you care to post one.

我知道 Postscript 的参考是左下角。我猜 PDF 是一样的,所以你从 y 值中减去以下降。在函数中打印开始和结束的“y”值以查看它们如何变化并根据句子的长度调整“y”值。函数如何知道“高度”是什么?我使用 ReportLab,但如果您愿意发布一个特定示例,可能会有所帮助。

回答by Alquimista

AutoReply:

自动回复:

def coord(x, y, height, unit=1):
    x, y = x * unit, height -  y * unit
    return x, y

w, h = table.wrap(width, height)
table.wrapOn(c, width, height)
table.drawOn(c, *coord(ml - 0.05, y + 4.6, height - h, cm))

the trick is in the "height - h", h is the height of the table and this depend of the content of the table

诀窍在于“高度 - h”,h 是表格的高度,这取决于表格的内容