reportlab中的自动换行

我正在使用drawCentredString在屏幕中央绘制一个字符串。但是,如果字符串大于“画布”的宽度,则它将超出框架。如何处理使其向下流动?

reportlab中的自动换行

guoyi110 回答:reportlab中的自动换行

reportlab.platypus.Paragraph自动将文本流到新行中。您必须将其与具有中心对齐的样式一起使用。

如果由于某种原因您不能使用Paragraph,则可以将内置的python模块textwrap与以下功能结合使用。

import textwrap

def draw_wrapped_line(canvas,text,length,x_pos,y_pos,y_offset):
    """
    :param canvas: reportlab canvas
    :param text: the raw text to wrap
    :param length: the max number of characters per line
    :param x_pos: starting x position
    :param y_pos: starting y position
    :param y_offset: the amount of space to leave between wrapped lines
    """
    if len(text) > length:
        wraps = textwrap.wrap(text,length)
        for x in range(len(wraps)):
            canvas.drawCenteredString(x_pos,wraps[x])
            y_pos -= y_offset
        y_pos += y_offset  # add back offset after last wrapped line
    else:
        canvas.drawCenteredString(x_pos,text)
    return y_pos
本文链接:https://www.f2er.com/3035657.html

大家都在问