Python--reportlab 生成PDF文档

引入reportlab库

from reportlab.lib import colors from reportlab.lib.pagesizes import letter from reportlab.lib.styles import getSampleStyleSheet from reportlab.platypus import SimpleDocTemplate, Table, TableStyle from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.lib.units import inchpdfmetrics.registerFont(TTFont('simsun', './simsun.ttf')) from reportlab.lib import fonts,colors from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer,Image,Table,TableStyle fonts.addMapping('simsun', 0, 0, 'simsun') doc = SimpleDocTemplate("weekly_report.pdf", pagesize=letter)

标题

story = [] stylesheet = getSampleStyleSheet() normalStyle = stylesheet['Normal'] rpt_title = '<para autoLeading="off" fontSize=15 align=center><b><font face="simsun">项目日报</font></b><br/><br/><br/></para>' story.append(Paragraph(rpt_title, normalStyle))

表格名称

text = '<para autoLeading="off" fontSize=9><br/><br/><br/><b><font face="simsun">当日授信信息表:</font></b><br/></para>' story.append(Paragraph(text, normalStyle))

表格内容

company_count = 0 checked_data_pdf.append(['企业名称', '模型等级', '推送等级', '模型额度', '推送额度', '状态标识', '时间']) for row in checked_table_pdf: checked_data_pdf.append(row) component_table = Table(checked_data_pdf, colWidths=[200, 50, 50, 50, 50,50,110]) component_table.setStyle(TableStyle([ ('FONTNAME', (0, 0), (-1, -1), 'simsun'), # 字体 ('FONTSIZE', (0, 0), (-1, -1), 10), # 字体大小 ('BACKGROUND', (0, 0), (8, 0), colors.lightskyblue), # 设置第一行背景颜色 ('LINEBEFORE', (0, 0), (0, -1), 0.1, colors.grey), # 设置表格左边线颜色为灰色,线宽为0.1 ('TEXTCOLOR', (0, 1), (-2, -1), colors.royalblue), # 设置表格内文字颜色 ('GRID', (0, 0), (-1, -1), 0.5, colors.red), # 设置表格框线为红色,线宽为0.5]))

    原文作者:志明S
    原文地址: https://www.jianshu.com/p/ce8a2af9fb77
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞