程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python uses reportlab to print pictures and texts

編輯:Python

This article mainly introduces printing simple report graphs , As shown in the figure below :

On the first code :

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 Eye chart diagnosis report of University Hospital ", style0))
story.append(Paragraph(" full name :XXX", style))
story.append(Paragraph(" Age :23", style))
story.append(Paragraph(" Gender : male ", style))
story.append(Paragraph("XX Diagnostic diagram :", style))
t = Image("E:\\1.png", width=300, height=200) ## Image path address
story.append(t)
story.append(Paragraph(" result : Everything is all right ", style))
story.append(Paragraph(" date :2022 year 6 month 15 Japan ", style))
story.append(Paragraph("XX University Hospital ", style1))
doc.build(story)

About ParagraphStyle The parameters of are as follows :

We select several important parameters to explain :

  • fontName: Font name
  • fontSize: font size
  • leading: Row spacing
  • leftIndent: Left
  • rightIndent: Indent right
  • firstLineIndent: The first line indentation
  • alignment: Alignment mode
  • spaceBefore: Pre segment clearance
  • spaceAfter: Post segment clearance
  • bulletFontName: List name
  • bulletFontSize: List font size
  • bulletIndent: Indent the list
  • textColor: The font color
  • backColor: Background color
  • borderWidth: Border thickness
  • borderPadding: Border spacing
  • borderColor: Border color

Description of formal parameters of fonts :

Here is what the system provides Normal Format , Actually Normal Format and ParagraphStyle It's exactly the same , except Normal Format , Other formats are also available .

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

Reference: 

  use Python Quickly and automatically generate pictures and texts PDF file - You know (zhihu.com)https://zhuanlan.zhihu.com/p/318390273

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved