添加段落样式报表实验室

2024-04-29 03:25:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试设置报告实验室的段落样式,我在此处定义了一种样式:

def stylesheet():
    styles= {
        'default': ParagraphStyle(
            'default',
            fontName='Arial',
            fontSize=16,
            leading=12,
            leftIndent=0,
            rightIndent=0,
            firstLineIndent=0,
            alignment=TA_LEFT,
            spaceBefore=0,
            spaceAfter=0,
            bulletFontName='Arial',
            bulletFontSize=10,
            bulletIndent=0,
            textColor= black,
            backColor=None,
            wordWrap=None,
            borderWidth= 0,
            borderPadding= 0,
            borderColor= None,
            borderRadius= None,
            allowWidows= 1,
            allowOrphans= 0,
            textTransform=None,  # 'uppercase' | 'lowercase' | None
            endDots=None,         
            splitLongWords=1,
        ),
    }

然后我就这样打印出来

   pdf = PDFDocument(carte)
    pdf.init_report()
    pdf.p(str(row))
    pdf.generate()

它提供未格式化的输出

当我试着

pdf = PDFDocument(carte)
pdf.init_report()
pdf.p(str(row), default)
pdf.generate()

若要将默认样式应用于我的文本,它会给出“NameError:name”“styles”未定义”

有线索吗?


Tags: reportnonedefaultpdfinit报告样式实验室
2条回答

使用reportlab尝试此操作,并将其添加到现有代码中:

from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.enums import TA_LEFT
from reportlab.lib.colors import black

styles['small'] = ParagraphStyle(
   'small',
    parent=styles['default'],
    fontSize=8,
    leading=8,
)

paragraphs.append(Paragraph('Text with default style<br/>', styles['default']))
paragraphs.append(Paragraph('Text with small style', styles['small']))

我已经挣扎了几个小时,到今天为止,提供的解决方案对我不起作用。我在programcreek上发现了另一个,差不多。稍作润色之后,这一个就成功了:

#First you need to instantiate 'getSampleStyleSheet()'
from reportlab.lib.styles import (ParagraphStyle, getSampleStyleSheet)
style = getSampleStyleSheet()
yourStyle = ParagraphStyle('yourtitle',
                           fontName="Helvetica-Bold",
                           fontSize=16,
                           parent=styles['Heading2'],
                           alignment=1,
                           spaceAfter=14)

要使用它,只需像这样调用yourStyle

Story.append(Paragraph("Whatever printed with yourStyle", yourStyle))

必须使用文档中所示的数字进行对齐:

There are four possible values of alignment, defined as constants in the module reportlab.lib.enums. These are TA_LEFT, TA_CENTER or TA_CENTRE, TA_RIGHT and TA_JUSTIFY, with values of 0, 1, 2 and 4 respectively. These do exactly what you would expect.

我之所以贴出答案,是因为我在任何地方都找不到确切的答案,希望能对其他人有所帮助。

相关问题 更多 >