Python教程

python Pdf 添加 文字 图片 线段 曲线 矩形 椭圆 例子

本文主要是介绍python Pdf 添加 文字 图片 线段 曲线 矩形 椭圆 例子,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
from reportlab.pdfbase.pdfmetrics import registerFont
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen.canvas import Canvas

pt = 72 / 25.4


class PdfShape:
    list_curve = []

    @classmethod
    def create_pdf(cls, name='demo1.pdf', pagesize=[210, 297], bleed=0, color={'c': 0, 'm': 0, 'y': 0, 'k': 1},
                   line_width=0.2, blank=20):
        w, h = pagesize[0] + blank * 2, pagesize[1] + blank * 2
        c = Canvas(name, trimBox=[bleed * pt, bleed * pt, (w - bleed) * pt,
                                  (h - bleed) * pt], pagesize=[w * pt, h * pt])
        c.setStrokeColorCMYK(c=color['c'], m=color['m'], y=color['y'], k=color['k'])
        c.setLineWidth(line_width)
        for i in PdfShape.list_curve:
            eval(i)
        c.save()

    @classmethod
    def create_pdf2(cls, name='demo1.pdf', pagesize=[210, 297]):

        c = Canvas(name, pagesize=pagesize)
        for i in PdfShape.list_curve:
            eval(i)
        c.save()

    @classmethod
    def add_obj(cls, text_obj):
        cls.list_curve.append(text_obj)


def draw_text(c, x=0, y=0, content='', fontname='E:/fonts/FZTCGHJW.TTF', fontsize=40, center=True, cc=0, mm=0, yy=0,
              kk=1):
    """
        添加文字
        eval("draw_text(c,105,70,'12522','E:/fonts/FZTCGHJW.TTF')")
    :return:
    """

    # 注册字体
    registerFont(TTFont('song', fontname))
    c.setFont('song', fontsize, leading=None)
    c.setFillColorCMYK(cc, mm, yy, kk)
    if center:
        c.drawCentredString(x, y, content)
    else:
        c.drawString(x, y, content)
    c.saveState()
    c.restoreState()


def draw_rect(c, x=0, y=0, width=40, height=20, stroke=1, fill=0):
    """
        画矩形
        eval("draw_rect(c,200,200,50,50)")
    :return:
    """

    c.rect(x, y, width, height, stroke=stroke, fill=fill)


def draw_ellipse(c, x1, y1, x2, y2, stroke=1, fill=0):
    """
        画椭圆
        eval("draw_ellipse(c,200,200,50,50)")
    :return:
    """

    c.ellipse(x1, y1, x2, y2, stroke=stroke, fill=fill)


def draw_line(c, x1, y1, x2, y2):
    """
        话线段
        eval("draw_line(c,200,200,50,50)")
    :return:
    """
    c.line(x1, y1, x2, y2)


def draw_polygon(c, positions, close, stroke=1, fill=0):
    """
        画多边形
        eval("draw_polygon(c,[(10,10),(120,120),(150,160)],True)")
    :return:
    """

    path = c.beginPath()
    x, y = positions[0]
    path.moveTo(x, y)
    for i, el in enumerate(positions, 1):
        path.lineTo(el[0], el[1])
    if close:
        path.close()
    c.drawPath(path, stroke=stroke, fill=fill)


def draw_curve(c, positions, close, stroke=1, fill=0):
    """
        画曲线
        "draw_curve(c,[(10,10),(120,120),(150,160)],True)"
    :return:
    """
    path = c.beginPath()
    x, y = positions[0]
    path.moveTo(x, y)
    for i, el in enumerate(positions, 1):
        if len(el) == 2:
            path.lineTo(el[0], el[1])
        elif len(el) == 6:
            path.curveTo(el[0], el[1], el[2], el[3], el[4], el[5])

    if close:
        path.close()
    c.drawPath(path, stroke=stroke, fill=fill)


def draw_image(c, image, x, y, w=None, h=None):
    """
        画图片
        eval("draw_image(c,200,200,50,50)")
    :return:
    """
    c.drawImage(image, x, y, w, h)


if __name__ == '__main__':
    """
        PdfShape.py 测试
    """
    PdfShape.add_obj("draw_image(c,'bilibili.png',0,0)")
    PdfShape.add_obj("draw_text(c,700,800,'12522','E:/fonts/FZTCGHJW.TTF')")
    PdfShape.add_obj("draw_polygon(c,[(500,500),(220,220),(350,360)],False)")
    PdfShape.add_obj("draw_curve(c,[(10,10),(120,120),(150,160)],True)")
    PdfShape.add_obj("draw_rect(c,200,200,50,50)")
    PdfShape.add_obj("draw_ellipse(c,200,200,50,50)")
    PdfShape.add_obj("draw_line(c,50,50,200,500)")
    PdfShape.create_pdf2(name='测试.pdf', pagesize=[1600, 900])

输出结果

在这里插入图片描述

这篇关于python Pdf 添加 文字 图片 线段 曲线 矩形 椭圆 例子的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!