Python USES reportlab drawing example (including Chinese characters)

  • 2020-04-02 13:15:02
  • OfStack

The preparatory work

Development environment: python2.6, reportlab

Prepare Chinese font file: simsun.ttc

Code:


#!/usr/bin/env python2.6
#coding:utf-8
import traceback
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.charts.textlabels import Label
from reportlab.graphics import renderPDF
from reportlab.graphics.widgets.markers import makeMarker
from reportlab.pdfbase import pdfmetrics, ttfonts
# Pay attention to data The type of 
# Each data point is a tuple 
# A curve corresponds to a tuple that stores a tuple of data points 
# A graph can contain more than one curve, using a list to store the curve tuples 
data=[((1,100),(2,200),(3,300),(4,400),(5,500)),((1,50),(2,80),(3,400),(4,40),(5,70))]
drawing = Drawing(500, 300)
lp = LinePlot()
lp.x = 50 # The center of the coordinate axis 
lp.y = 30
lp.height = 250
lp.width = 400
lp.data = data
lp.joinedLines = 1
lp.lines.symbol = makeMarker('FilledCircle')
lp.xValueAxis.valueMin = 1
lp.xValueAxis.valueMax = 5
lp.xValueAxis.valueStep = 1
lp.yValueAxis.valueMin = 0
lp.yValueAxis.valueMax = 500
lp.yValueAxis.valueStep = 100
drawing.add(lp)
title = Label()
# If you want to display Chinese, you need to register a Chinese font 
pdfmetrics.registerFont(ttfonts.TTFont("haha", "simsun.ttc"))
title.fontName   = "haha"
title.fontSize   = 12
title_text = unicode(' hello ','gbk')
#title_text = "abc"
title._text = title_text
title.x          = 250
title.y          = 280
title.textAnchor ='middle'
drawing.add(title)
Xlabel = Label()
Xlabel._text = 'x'
Xlabel.fontSize   = 12
Xlabel.x          = 480
Xlabel.y          = 30
Xlabel.textAnchor ='middle'
drawing.add(Xlabel)
Ylabel = Label()
Ylabel._text = "y"
Ylabel.fontSize   = 12
Ylabel.x          = 40
Ylabel.y          = 295
Ylabel.textAnchor ='middle'
drawing.add(Ylabel)

try:
     drawing.save(formats=['gif'],outDir=".",fnRoot="abc")
except:
     traceback.print_exc()


Related articles: