引入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]))