python 报告实验室 PDF 表格上的自动换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2121909/
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-11-03 23:51:27  来源:igfitidea点击:

Word wrap on report lab PDF table

pythonreportlab

提问by Pedro Ghilardi

I'm using the Table of Report Lab library to print a table on a PDF report. I would like to know if it's possible to configure the table to perform an automatic wrapping of the content of a cell.

我正在使用 Table of Report Lab 库在 PDF 报告上打印表格。我想知道是否可以配置表格以执行单元格内容的自动换行。

For example, I have a text that doesn't fit on a cell inside a column. I would like that the table performs the wrap automatically adjusting the content of the cells to fit on the columns width. Is it possible?

例如,我有一个不适合列内单元格的文本。我希望表格执行自动换行,调整单元格的内容以适应列宽。是否可以?

回答by David Raznick

You can put any flowable in a table element. It is probably good practice to have all table elements as flowables, so they can be styled the same. For your case you will most likely need a Paragraph flowable. eg.

您可以将任何可流动的元素放入表格元素中。将所有表格元素都作为可流动对象可能是一种很好的做法,因此它们的样式可以相同。对于您的情况,您很可能需要一个可流动的段落。例如。

styles = getSampleStyleSheet()
text = Paragraph("long line",
              styles['Normal'])

You can put `text' into the data you feed to a table and it will automatically wrap.

您可以将“文本”放入您提供给表格的数据中,它会自动换行。

回答by pako anguiano

My solution, force newline in the string:

我的解决方案,在字符串中强制换行:

def __chopLine(line, maxline):

    cant = len(line) / maxline
    cant += 1
    strline = ""
    index = maxline
    for i in range(1,cant):
        index = maxline * i
        strline += "%s\n" %(line[(index-maxline):index])
    strline += "%s\n" %(line[index:])
    return strline

回答by Sourabh Gupta

*whole code of word wrap

*自动换行的完整代码

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
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

# bodytext  style used for wrapping  data on flowables 
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
#used alignment if required
styleN.alignment = TA_LEFT

styleBH = styles["Normal"]
styleBH.alignment = TA_CENTER


hdescrpcion = Paragraph('''<b>descrpcion</b>''', styleBH)
hpartida = Paragraph('''<b>partida</b>''', styleBH)


descrpcion = Paragraph('long long long long long long long long long long long long long long long long long long long long line ', styleN)
partida = Paragraph('1', styleN)

data= [[hdescrpcion, hpartida],
       [partida ,descrpcion]]

table = Table(data)

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, 50, 50)
table.drawOn(c, 100,600)
c.save()