如何在reportlab选项卡中对齐单个单元格

2024-06-16 12:30:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用创建一个表,我要将下表中的单个单元格(向右)对齐:

我要把包含“占领”的单元格向右对齐

这是我的代码:

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]

Tags: no代码nameapplicationleftreportlabcourseuserdata
1条回答
网友
1楼 · 发布于 2024-06-16 12:30:24

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

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

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

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

段落示例

^{pr2}$

相关问题 更多 >