ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-01-05 16:34:27  阅读:477  来源: 互联网

标签:reportlab Python leading elif stylesheet values 设置 new op


可以设置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

标签:reportlab,Python,leading,elif,stylesheet,values,设置,new,op
来源: https://www.cnblogs.com/qianxunman/p/14236566.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有