gpt4 book ai didi

python - 添加段落样式reportlab

转载 作者:行者123 更新时间:2023-12-01 03:48:31 24 4
gpt4 key购买 nike

我正在尝试设置报告实验室的段落样式,我在这里定义了一个样式:

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:名称'样式'未定义”

有什么线索吗?

最佳答案

我已经为此苦苦挣扎了几个小时,截至今天,提供的解决方案对我来说不起作用。我在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=style['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.

我发布答案是因为我在任何地方都找不到准确的答案,希望它可以帮助其他人。

关于python - 添加段落样式reportlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38557119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com