python利用reportlab打印图文并茂内容

本文主要介绍打印简单的报告图,如下图示:

先上代码:

from reportlab.platypus import SimpleDocTemplate, Image, Paragraph
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
pdfmetrics.registerFont(TTFont('SimKai', 'D:\\Anaconda3\\envs\\torch1.5\\Lib\\site-packages\\reportlab\\fonts\\SimKai.ttf'))
doc = SimpleDocTemplate("Hello1.pdf")
styles = getSampleStyleSheet()
style0 = styles["Title"]
style0.fontName = "SimKai"
style = styles['Normal']
style.fontName = "SimKai"
style1 = styles["Heading3"]
style1.fontName = "SimKai"
style1.alignment = 1

story =[]
story.append(Paragraph("XX大学校医院眼图诊断报告", style0))
story.append(Paragraph("姓名:XXX", style))
story.append(Paragraph("年龄:23", style))
story.append(Paragraph("性别:男", style))
story.append(Paragraph("XX诊断图示:", style))
t = Image("E:\\1.png", width=300, height=200)  ##图片路径地址
story.append(t)
story.append(Paragraph("结果:一切正常", style))
story.append(Paragraph("日期:2022年6月15日", style))
story.append(Paragraph("XX大学校医院", style1))
doc.build(story)


关于ParagraphStyle的参数说明如下:

我们选取几个重要的参数进行说明:

  • fontName:字体名称
  • fontSize:字体大小
  • leading:行间距
  • leftIndent:左缩进
  • rightIndent:右缩进
  • firstLineIndent:首行缩进
  • alignment:对齐方式
  • spaceBefore:段前间隙
  • spaceAfter:段后间隙
  • bulletFontName:列表名称
  • bulletFontSize:列表字体大小
  • bulletIndent:列表缩进
  • textColor:字体颜色
  • backColor:背景色
  • borderWidth:边框粗细
  • borderPadding:边框间距
  • borderColor:边框颜色

关于字体的形式参数说明:

这里就是获得系统提供的Normal格式,其实Normal格式与ParagraphStyle是一模一样的,除了Normal格式,还可以获得其他的格式。

  • Normal
  • BodyText
  • Italic
  • Heading1
  • Title
  • Heading2
  • Heading3
  • Heading4
  • Heading5
  • Heading6
  • Bullet
  • Definition
  • Code
  • UnorderedList
  • OrderedList

Reference: 

 用Python快速自动生成图文并茂的PDF文件 - 知乎 (zhihu.com)icon-default.png?t=M4ADhttps://zhuanlan.zhihu.com/p/318390273

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心之所向521

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值