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

发布于 2024-08-18 16:43:09 字数 119 浏览 10 评论 0原文

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

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停滞 2024-08-25 16:43:09

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

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

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

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.

平定天下 2024-08-25 16:43:09

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

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

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
森罗 2024-08-25 16:43:09

*自动换行的完整代码

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

*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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文