python – 一个段落中的Reportlab粗体和普通文本
作者:互联网
我正在使用reportlab(与Django一起)创建PDF.我创建了以下用于创建包含头部和细节的表的代码:
elements = []
datas = []
course_info = [
['Course Code' , c.course_code] ,
['Course Title' , c.course_name],
['Prerequisites by Course(s) and Topics', c.pre_reqs],
['Assessment Instruments with Weights (some desc)', c.grade_distribution]
]
for k in course_info:
headpara = Paragraph(k[0], styleB)
datas.append([headpara , Paragraph(clean_string(k[1]), styleN)])
t = LongTable(datas, colWidths=[5 * cm, 12 * cm])
t.setStyle(TableStyle(org.getTableStyle()))
elements.append(t)
doc.build(elements)
我正在使用BaseDocTemplate作为我的模板.我想要的是能够将headpara的一部分作为非粗体,例如第四行中的(某些desc)部分需要是普通样式而不是粗体.我怎样才能做到这一点?
解决方法:
Reportlab支持简单的HTML格式化,因此我们可以使用styleN并使所需的文本变为粗体.像这样:
course_info = [
['<b>Course Code</b>' , c.course_code] ,
['<b>Course Title</b>' , c.course_name],
['<b>Prerequisites by Course(s) and Topics</b>', c.pre_reqs],
['<b>Assessment Instruments with Weights</b> (some desc)', c.grade_distribution]
]
for k in course_info:
headpara = Paragraph(k[0], styleN)
datas.append([headpara , Paragraph(clean_string(k[1]), styleN)])
标签:reportlab,python,formatting 来源: https://codeday.me/bug/20190901/1783353.html