引入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)
標(biāo)題
story = [] stylesheet = getSampleStyleSheet() normalStyle = stylesheet['Normal'] rpt_title = '<para autoLeading="off" fontSize=15 align=center><b><font face="simsun">項(xiàng)目日?qǐng)?bào)</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">當(dāng)日授信信息表:</font></b><br/></para>' story.append(Paragraph(text, normalStyle))
表格內(nèi)容
company_count = 0 checked_data_pdf.append(['企業(yè)名稱', '模型等級(jí)', '推送等級(jí)', '模型額度', '推送額度', '狀態(tài)標(biāo)識(shí)', '時(shí)間']) 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), # 設(shè)置第一行背景顏色 ('LINEBEFORE', (0, 0), (0, -1), 0.1, colors.grey), # 設(shè)置表格左邊線顏色為灰色一膨,線寬為0.1 ('TEXTCOLOR', (0, 1), (-2, -1), colors.royalblue), # 設(shè)置表格內(nèi)文字顏色 ('GRID', (0, 0), (-1, -1), 0.5, colors.red), # 設(shè)置表格框線為紅色,線寬為0.5]))