如何在 reportlab 中对齐单个单元格 Table

How to align a single cell in reportlab Table

我正在用 创建一个 table,我想在下面 table:

中对齐一个单元格(向右)

我想将包含 "Occupation" 的单元格右对齐

这是我的代码:

studentProfileData = [
                ['Application Form No', ''],
                ['Name', userData['studentDetails']["firstName"] + " " +userData['studentDetails']["lastName"]],
                ['Course opted for', userData['courseDetails']["courseOptedFor"]],
                ['Specific Course Name', courseMapping["Name"]],
                ['Category', userData['studentDetails']['caste']],
                ['Religion', userData['studentDetails']['religion']],
                ['Fathers'+ "'" +'s Name', userData['studentDetails']['religion']],
                ['Occupation', userData['studentDetails']['fOccupation']],
                ['Phone No', ""],
                ['Term', ""]
            ]

        colwidths = [3 * inch, 1.5 * inch, inch]

        # Two rows with variable height
        rowheights = [.5*inch] * len(studentProfileData)
        studentProfile = Table(studentProfileData, colwidths, rowheights, hAlign='LEFT')

        studentProfile.setStyle(TableStyle([
                ('ALIGN', (0, 0), (0, -1), "LEFT"),
                ('FONTSIZE', (0,0), (-1, -1), 13),
            ]))

        parts = [ page1Head, studentProfile]

为了在 Reportlab Table 中对齐单个单元格,我们需要将 TableStyle 更改为以下内容:

TableStyle([
            ('ALIGN', (0, 0), (0, -1), "LEFT"),
            ('FONTSIZE', (0,0), (-1, -1), 13),
            ('ALIGN', (0, 7), (0, 7), "RIGHT"),
        ])

这是有效的,因为我们现在告诉 (0,7)(0,7) 之间的区域中的单元格应该右对齐,因为该区域中唯一的单元格是仅包含 Occupation 的单元格该文本已对齐。

另一种方法是在 table 中使用 Paragraph 而不是仅使用 String,在这种情况下,我们可以使用 Paragraph 进行对齐它将填满单元格的整个宽度。

段落示例

pageTextStyleCenter = ParagraphStyle(name="left", alignment=TA_CENTER, fontSize=13, leading=10)

[ Paragraph("Occupation", pageTextStyleCenter) , userData['studentDetails'].get('fOccupation', "-")]