Python reportlab table 设置cellstyle枚举,设置单元格padding

可以设置cellsyle 的选项:


def _setCellStyle(cellStyles, i, j, op, values):
    #new = CellStyle('<%d, %d>' % (i,j), cellStyles[i][j])
    #cellStyles[i][j] = new
    ## modify in place!!!
    new = cellStyles[i][j]
    if op == 'FONT':
        n = len(values)
        new.fontname = values[0]
        if n>1:
            new.fontsize = values[1]
            if n>2:
                new.leading = values[2]
            else:
                new.leading = new.fontsize*1.2
    elif op in ('FONTNAME', 'FACE'):
        new.fontname = values[0]
    elif op in ('SIZE', 'FONTSIZE'):
        new.fontsize = values[0]
    elif op == 'LEADING':
        new.leading = values[0]
    elif op == 'TEXTCOLOR':
        new.color = colors.toColor(values[0], colors.Color(0,0,0))
    elif op in ('ALIGN', 'ALIGNMENT'):
        new.alignment = values[0]
    elif op == 'VALIGN':
        new.valign = values[0]
    elif op == 'LEFTPADDING':
        new.leftPadding = values[0]
    elif op == 'RIGHTPADDING':
        new.rightPadding = values[0]
    elif op == 'TOPPADDING':
        new.topPadding = values[0]
    elif op == 'BOTTOMPADDING':
        new.bottomPadding = values[0]
    elif op == 'HREF':
        new.href = values[0]
    elif op == 'DESTINATION':
        new.destination = values[0]

设置方式:

    @classmethod
    def xxx(cls, internal_obj):
        move_lines_list = cls.get_move_lines_list(internal_obj)

        pdfmetrics.registerFont(TTFont('SimSun', cls.SIMSUN_PATH))  # 注册字体
        pdfmetrics.registerFont(TTFont('msyh', cls.MSYH_PATH))  # 注册字体
        stylesheet = getSampleStyleSheet()
        # stylesheet.add(ParagraphStyle(fontName='msyh', name='Hei', leading=2, fontSize=8, alignment=1))
        # stylesheet.add(ParagraphStyle(fontName='msyh', name='Hei2', leading=1, fontSize=8, alignment=0))
        # stylesheet.add(ParagraphStyle(fontName='msyh', name='Hei3', leading=1, fontSize=8, alignment=0))
        stylesheet.add(ParagraphStyle(fontName='msyh', name='Hei',alignment=2, fontSize=7,leading=0,rightIndent=0)) # RIGHT
        stylesheet.add(ParagraphStyle(fontName='msyh', name='Hei1',alignment=1, fontSize=8,leading=0)) # CENTER
        stylesheet.add(ParagraphStyle(fontName='msyh', name='Hei0',alignment=0, fontSize=7,leading=0)) # LEFT
        cellstyle = CellStyle(name='nopadding')
        cellstyle.rightPadding=0
        elements = []
        for _data in move_lines_list:
            num = _data.get("num", 0)
            code = _data.get("barcode")
            for i in range(num):
                code_obj = code128.Code128('{}\x0d'.format(code), barHeight=7 * mm, barWidth=0.99)
                data = [
                    [code_obj],
                    # [],
                    [Paragraph(code, stylesheet['Hei1'])],
                    [Paragraph('NEW', stylesheet['Hei0']), Paragraph('MADE IN CHINA', stylesheet['Hei'])],
                ]
                t = Table(data, colWidths=[21.5 * mm],rowHeights=[9 * mm, 5 * mm, 5 * mm])


                t.setStyle([
                    ('VALIGN', (0, 0), (-1, -1), 'TOP'),
                    ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
                    ('GRID', (0, 0), (-1, -1), 0.5, colors.grey),  # 设置表格框线为grey色,线宽为0.5
                    ('SPAN', (0, 0), (1, 0)),
                    ('SPAN', (0, 1), (1, 1)),
                    ('RIGHTPADDING', (0, 0), (-1, -1), 0),
                    ('LEFTPADDING', (0, 0), (-1, -1), 0),
                    # ('VALIGN', (0, 0), (-1, -1), 'TOP'),
                    # ('ALIGN', (0, 2), (1, 2), 'LEFT'),
                ])

                elements.append(t)
                elements.append(PageBreak())
        response = HttpResponse(content_type='application/pdf')
        response['Content-Disposition'] = 'filename="poland_label.pdf"'
        # label = SimpleDocTemplate(response, pagesize=(cls.PAGE_WIDTH_S, cls.PAGE_HEIGHT_S), rightMargin=0,
        #                           leftMargin=0,
        #                           topMargin=2, bottomMargin=0)
        label = SimpleDocTemplate('test.pdf', pagesize=(cls.PAGE_WIDTH_S, cls.PAGE_HEIGHT_S), rightMargin=0,
                                  leftMargin=0,
                                  topMargin=2, bottomMargin=0)
        label.build(elements)
        # return response
上一篇:python读取excel文件单元格中的数据返回类型


下一篇:P2存储程序原理