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

网友投稿 1015 2022-10-11

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

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-Padding = 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

懂得,原来世界如此简单!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:elasticsearch的灵魂唯一master选举机制原理分析
下一篇:微信小程序加密数据解密算法Go版
相关文章

 发表评论

暂时没有评论,来抢沙发吧~