python添加【文字,矩形,椭圆,线条,曲线】

经验创意 · 249 次浏览
jialan75home 创建于 2023-12-13 10:25

引用到的包
pip install reportlab -i https://pypi.douban.com/simple/

 

from reportlab.pdfgen import canvas

from reportlab.pdfbase import pdfmetrics, ttfonts

import os


class MyCanvas(canvas.Canvas):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# self.w = w
# self.h = h
# self.setPageSize(self.w, self.h)

def draw_line(self, points, line_width=1, stroke_color=(0, 0, 0, 1)):
"""
使用例子 draw_line([(50, 50), (100, 100)], line_width=2, stroke_color=(1, 0, 0, 1))
:param points: 必须是是2个点的坐标
:param line_width: 描边宽度
:param stroke_color: 描边颜色
:return:
"""
self.setStrokeColorCMYK(*stroke_color)
self.setLineWidth(line_width)
x1, y1 = points[0]
x2, y2 = points[1]
self.line(x1, y1, x2, y2)

def draw_polygon(self, points, stroke_width=1, stroke_color=(0, 0, 0, 1)):
"""
使用例子 draw_polygon([(0, 0), (50, 0), (50, 50), (0, 50)], stroke_width=2, stroke_color=(0, 1, 0, 1))
:param points: 第一个点的坐标为 起始坐标,后面的坐标都是相对于第一点坐标 画图
:param stroke_width: 描边宽度
:param stroke_color: 描边颜色
:return:
"""
self.setStrokeColorCMYK(*stroke_color)
self.setLineWidth(stroke_width)
path = self.beginPath()
x, y = points[0]
path.moveTo(x, y)
for point in points[1:]:
path.lineTo(x + point[0], y + point[1])
path.lineTo(x, y)
self.drawPath(path, stroke=1)

def draw_ellipse(self, x, y, width, height, stroke_width=1, stroke_color=(0, 0, 0, 1)):
"""
使用例子 draw_ellipse(50, 50, 100, 100, stroke_width=2, stroke_color=(1, 0, 0, 1))
:param x: 起始点坐标X
:param y: 起始点坐标Y
:param width: 椭圆宽度
:param height: 椭圆高度
:param stroke_width: 描边宽度
:param stroke_color: 描边颜色
:return:
"""
self.setStrokeColorCMYK(*stroke_color)
self.setLineWidth(stroke_width)
self.ellipse(x, y, x + width, y + height, stroke=1)

def draw_rectangle(self, x, y, width, height, stroke_width=1, stroke_color=(0, 0, 0, 1)):
"""
使用例子 draw_rectangle(50, 50, 100, 100, stroke_width=2, stroke_color=(1, 0, 0, 1))
:param x: 起始点坐标X
:param y: 起始点坐标Y
:param width: 矩形宽度
:param height: 矩形高度
:param stroke_width: 描边宽度
:param stroke_color: 描边颜色
:return:
"""
self.setStrokeColorCMYK(*stroke_color)
self.setLineWidth(stroke_width)
self.rect(x, y, width, height, stroke=1)

def draw_curve(self, points, stroke_width=1, stroke_color=(0, 0, 0, 1), closed=True, relative_coords=True):
"""
使用例子
points = [(0, 0), (50, 50), (100, 150), (150, 50), (200, 200), (250, 50), (200, 150)]
draw_curve(points, stroke_width=2, stroke_color=(0, 0.5, 0, 0))
:param points:
当 relative_coords = True时 第一点为基准坐标,后面的坐标都是相对于第一点的坐标
当 relative_coords = Flase时 所有坐标点都是绝对坐标
:param stroke_width: 描边宽度
:param stroke_color: 描边颜色
:param closed: 是否闭合
:param relative_coords: 是否为相对坐标
:return:
"""
x0, y0 = points[0]
if relative_coords:
points = [(x + points[0][0], y + points[0][1]) for x, y in points]
self.setStrokeColorCMYK(*stroke_color)
self.setLineWidth(stroke_width)
path = self.beginPath()
path.moveTo(x0, y0)
for (x1, y1), (x2, y2), (x3, y3) in [points[i:i + 3] for i in range(1, len(points), 3)]:
path.curveTo(x1, y1, x2, y2, x3, y3)
if closed:
path.lineTo(x0, y0)
self.drawPath(path, stroke=True)

def draw_text(self, x, y, text, font_size=12, font_name="msyh", text_color=(0, 0, 0, 1)):
# 判断 font_name是不是路径
if os.path.isfile(font_name):
# 假设font_name = r'F:\python_box\msyh.ttf'
# 获取文件名名称 (filename = 'msyh')
filename = os.path.splitext(os.path.basename(font_name))[0]
# 判断该名称是否在字体库里面 如果不存在就 注册该字体
if filename not in pdfmetrics.getRegisteredFontNames():
# 注册字体
pdfmetrics.registerFont(ttfonts.TTFont(filename, font_name))
self.setFont(filename, font_size)
else:
self.setFont(font_name, font_size)
self.setFillColorCMYK(*text_color)
self.drawString(x, y, text)

my_canvas = MyCanvas('example.pdf')

my_canvas.draw_line([(50, 50), (100, 100)], line_width=2, stroke_color=(1, 0, 0, 1))
my_canvas.draw_ellipse(50, 50, 100, 100, stroke_width=2, stroke_color=(1, 0, 0, 1))
my_canvas.draw_rectangle(50, 50, 100, 100, stroke_width=2, stroke_color=(1, 0, 0, 1))
my_canvas.draw_polygon([(100, 100), (50, 0), (50, 50), (0, 50)], stroke_width=2, stroke_color=(0, 1, 0, 1))

points = [(0, 0), (50, 50), (100, 150), (150, 50), (200, 200), (250, 50), (200, 150)]
my_canvas.draw_curve(points, stroke_width=2, stroke_color=(0, 0.5, 0, 0))

my_canvas.draw_text(50, 180, 'jialan75,你在就好了22', 12, r'F:\python_box\msyh.ttf')
my_canvas.draw_text(50, 200, 'jialan75,你在就好了', 12, r'F:\python_box\msyh.ttf')

my_canvas.showPage()
my_canvas.save()

回复内容
暂无回复
回复主贴