我正在使用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)部分需要是普通样式而不是粗体。我怎样才能做到这一点?

最佳答案

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)])

10-05 21:52