python源码大全reportlab教程和源码大全

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

2019-08-08  本文已影响1人  iCloudEnd

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

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

上期精彩

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

上一篇下一篇

猜你喜欢

热点阅读