Python ReportLab-表上的Wordwrap是分裂单词而不是在空格上

发布于 2025-01-23 04:52:14 字数 2921 浏览 0 评论 0原文

我使用Canvas在ReportLab中创建了PDF:

self.pdf = canvas.Canvas(f'{file_name}.pdf', pagesize=A4)

我在表中创建表格以创建文档,但我的一张表并未按照我的期望包装。它不是在空间上闯入线路,而是在单词之间进行,如下所示。

下面的代码是我用于创建表的代码。只要我确保要传递到表()的单元格是段落()的时间。

def _discount_table(self, width_list):
    # Table Name
    table_name = Paragraph('DISCOUNTS', self.header_style_grey)

    # Create Header
    header = [Paragraph('NAME', self.table_header_style_left)]
    header += [Paragraph(x, self.table_header_style_right) for x in self.unique_discount_list]
    header += [Paragraph('TOTAL', self.table_header_style_right)]

    # Process Data
    discount_data = [[Paragraph(cell, self.table_style2) for cell in row] for row in self.discount_data]
    data = [[child_row[0]] + disc for child_row, disc in zip(self.fees_data, discount_data)]

    # Create Footer
    table_footer = [Paragraph('') for _ in range(len(header) - 2)]
    table_footer += [Paragraph('TOTAL', self.table_header_style_right),
                     Paragraph(f'{self.discount_total:,.2f}', self.table_header_style_right)]

    # Create Table
    bg_color = self.header_style_grey.textColor
    table = Table([header] + data + [table_footer], colWidths=width_list)
    table.setStyle([
        ('GRID', (0, 0), (-1, -1), 1, 'black'),
        ('BACKGROUND', (0, 0), (-1, 0), bg_color),
        ('TEXTCOLOR', (0, 0), (-1, 0), 'white'),
        ('BACKGROUND', (-2, -1), (-1, -1), bg_color),
        ('TEXTCOLOR', (-2, -1), (-1, -1), 'white'),
        ('FONTNAME', (-2, -1), (-1, -1), 'Helvetica-Bold'),
        ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
        ('ALIGN', (1, 0), (-1, -1), 'RIGHT'),
        ('ROWBACKGROUNDS', (0, 1), (-1, -2), ['lightgrey', 'white']),
    ])

    return [table_name, table]

(请注意,Child_row [0]已经是段落 - 这是在上面的第12行中找到的)

我使用的造型是从另一个Python文件导入的:

    self.table_style2 = ParagraphStyle('table_style')
    self.table_style2.wordWrap = 'CJK'
    self.table_style2.alignment = TA_RIGHT

    self.table_style = ParagraphStyle('table_style')
    self.table_style.wordWrap = 'CJK'

    self.table_header_style_right = ParagraphStyle('table_header_style', self.table_style)
    self.table_header_style_right.textColor = colors.HexColor('#FFFFFF')
    self.table_header_style_right.fontName = 'Helvetica-Bold'
    self.table_header_style_right.alignment = TA_RIGHT
    self.table_header_style_right.wordWrap = 'CJK'

    self.table_header_style_left = ParagraphStyle('table_header_style', self.table_style)
    self.table_header_style_left.textColor = colors.HexColor('#FFFFFF')
    self.table_header_style_left.fontName = 'Helvetica-Bold'
    self.table_header_style_left.wordWrap = 'CJK'

因此,我真的丢失了,需要帮助。为什么表不正确包裹?

I created a PDF in reportlab using a canvas:

self.pdf = canvas.Canvas(f'{file_name}.pdf', pagesize=A4)

I create tables within tables to create my document but one of my tables is not wrapping the way I expect it to. Rather than linebreaking at spaces, it does so between words as seen below.
enter image description here

The code below is the code I used to create the table. It is a bit long as I did make sure that the cells I'm passing into the Table() are all Paragraph().

def _discount_table(self, width_list):
    # Table Name
    table_name = Paragraph('DISCOUNTS', self.header_style_grey)

    # Create Header
    header = [Paragraph('NAME', self.table_header_style_left)]
    header += [Paragraph(x, self.table_header_style_right) for x in self.unique_discount_list]
    header += [Paragraph('TOTAL', self.table_header_style_right)]

    # Process Data
    discount_data = [[Paragraph(cell, self.table_style2) for cell in row] for row in self.discount_data]
    data = [[child_row[0]] + disc for child_row, disc in zip(self.fees_data, discount_data)]

    # Create Footer
    table_footer = [Paragraph('') for _ in range(len(header) - 2)]
    table_footer += [Paragraph('TOTAL', self.table_header_style_right),
                     Paragraph(f'{self.discount_total:,.2f}', self.table_header_style_right)]

    # Create Table
    bg_color = self.header_style_grey.textColor
    table = Table([header] + data + [table_footer], colWidths=width_list)
    table.setStyle([
        ('GRID', (0, 0), (-1, -1), 1, 'black'),
        ('BACKGROUND', (0, 0), (-1, 0), bg_color),
        ('TEXTCOLOR', (0, 0), (-1, 0), 'white'),
        ('BACKGROUND', (-2, -1), (-1, -1), bg_color),
        ('TEXTCOLOR', (-2, -1), (-1, -1), 'white'),
        ('FONTNAME', (-2, -1), (-1, -1), 'Helvetica-Bold'),
        ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
        ('ALIGN', (1, 0), (-1, -1), 'RIGHT'),
        ('ROWBACKGROUNDS', (0, 1), (-1, -2), ['lightgrey', 'white']),
    ])

    return [table_name, table]

(To note that child_row[0] is already a Paragraph - this is found on the line 12 above)

The styling I used is imported from another python file as follows:

    self.table_style2 = ParagraphStyle('table_style')
    self.table_style2.wordWrap = 'CJK'
    self.table_style2.alignment = TA_RIGHT

    self.table_style = ParagraphStyle('table_style')
    self.table_style.wordWrap = 'CJK'

    self.table_header_style_right = ParagraphStyle('table_header_style', self.table_style)
    self.table_header_style_right.textColor = colors.HexColor('#FFFFFF')
    self.table_header_style_right.fontName = 'Helvetica-Bold'
    self.table_header_style_right.alignment = TA_RIGHT
    self.table_header_style_right.wordWrap = 'CJK'

    self.table_header_style_left = ParagraphStyle('table_header_style', self.table_style)
    self.table_header_style_left.textColor = colors.HexColor('#FFFFFF')
    self.table_header_style_left.fontName = 'Helvetica-Bold'
    self.table_header_style_left.wordWrap = 'CJK'

So I am really lost and need help. Why is the table not wrapping correctly?

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

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

发布评论

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

评论(1

御守 2025-01-30 04:52:15

当我删除代码的WordWrap ='cjk'部分时,我能够修复表包裹问题。我在视频中看到了段落()将自动wordwrap,所以我猜这两个元素如何重叠存在一些问题

self.table_style2 = ParagraphStyle('table_style')
# self.table_style2.wordWrap = 'CJK'
self.table_style2.alignment = TA_RIGHT

self.table_style = ParagraphStyle('table_style')
# self.table_style.wordWrap = 'CJK'

self.table_header_style_right = ParagraphStyle('table_header_style', self.table_style)
self.table_header_style_right.textColor = colors.HexColor('#FFFFFF')
self.table_header_style_right.fontName = 'Helvetica-Bold'
self.table_header_style_right.alignment = TA_RIGHT
# self.table_header_style_right.wordWrap = 'CJK'

self.table_header_style_left = ParagraphStyle('table_header_style', self.table_style)
self.table_header_style_left.textColor = colors.HexColor('#FFFFFF')
self.table_header_style_left.fontName = 'Helvetica-Bold'
# self.table_header_style_left.wordWrap = 'CJK'

I was able to fix the table wrap issue when I removed the wordWrap = 'CJK' portion of the code. I saw in a video that a Paragraph() will automatically wordWrap so I'm guessing there was some issue with how those two elements overlap

self.table_style2 = ParagraphStyle('table_style')
# self.table_style2.wordWrap = 'CJK'
self.table_style2.alignment = TA_RIGHT

self.table_style = ParagraphStyle('table_style')
# self.table_style.wordWrap = 'CJK'

self.table_header_style_right = ParagraphStyle('table_header_style', self.table_style)
self.table_header_style_right.textColor = colors.HexColor('#FFFFFF')
self.table_header_style_right.fontName = 'Helvetica-Bold'
self.table_header_style_right.alignment = TA_RIGHT
# self.table_header_style_right.wordWrap = 'CJK'

self.table_header_style_left = ParagraphStyle('table_header_style', self.table_style)
self.table_header_style_left.textColor = colors.HexColor('#FFFFFF')
self.table_header_style_left.fontName = 'Helvetica-Bold'
# self.table_header_style_left.wordWrap = 'CJK'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文