3

我正在使用 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为非粗体,例如(some desc)第四行中的部分需要是普通样式而不是粗体。我怎样才能做到这一点?

4

1 回答 1

7

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)])
于 2012-12-16T05:46:36.343 回答