如何对齐reportlab表中的单个单元格

byt*_*0de 2 python django pdf-generation reportlab

创建一个表格,我想在下表中对齐单个单元格(向右):

我想将包含“职业”的单元格向右对齐

这是我的代码:

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]
Run Code Online (Sandbox Code Playgroud)

B8v*_*ede 7

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

TableStyle([
            ('ALIGN', (0, 0), (0, -1), "LEFT"),
            ('FONTSIZE', (0,0), (-1, -1), 13),
            ('ALIGN', (0, 7), (0, 7), "RIGHT"),
        ])
Run Code Online (Sandbox Code Playgroud)

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

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

段落示例

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

[ Paragraph("Occupation", pageTextStyleCenter) , userData['studentDetails'].get('fOccupation', "-")]
Run Code Online (Sandbox Code Playgroud)