Reportlab基础教程02之如何绘制字符串(含代码)

字符串绘制是操作PDF的基本功,本文将给大家介绍几种常用的绘制函数

  • 标准绘制函数canvas.drawString(80, 700, 'Standard String')
  • x轴右对齐绘制函数canvas.drawRightString(80, 680, 'Right String')
  • 轴对称绘制函数canvas.drawAlignedString(60, y, str(number))
  • 中心对称绘制函数canvas.drawCentredString(width / 2, 550, 'Centered String')

demo 源码

# string_alignment.py

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter


def string_alignment(my_canvas):
    width, height = letter
    
    my_canvas.drawString(80, 700, 'Standard String')
    my_canvas.drawRightString(80, 680, 'Right String')
    
    numbers = [987.15, 42, -1,234.56, (456.78)]
    y = 650
    for number in numbers:
        my_canvas.drawAlignedString(60, y, str(number))
        y -= 20
    
    my_canvas.drawCentredString(width / 2, 550, 'Centered String')
    
    my_canvas.showPage()
    

if __name__ == '__main__':
    my_canvas = canvas.Canvas("string_alignment.pdf")
    string_alignment(my_canvas)
    my_canvas.save()

运行效果

guzicha.com

上期精彩

  • reportlab基础教程之pdfgen和基础单位介绍(含源码)
  • reportlab基础教程01之生成多页pdf

更多精彩代码请关注我的专栏

  • reportlab教程和源码大全

  • python源码大全

你可能感兴趣的:(Reportlab基础教程02之如何绘制字符串(含代码))