python reportlab模块----操作PDF文件

reportlab模块----操作PDF文件

  • [一. 安装模块](#一. 安装模块)
  • [二. reportlab相关介绍](#二. reportlab相关介绍)
  • [三. 扩展canvas类](#三. 扩展canvas类)
  • [四. 水平写入完整代码](#四. 水平写入完整代码)
  • [五. 垂直写入完整代码](#五. 垂直写入完整代码)

一. 安装模块

shell 复制代码
pip install reportlab

二. reportlab相关介绍

python 复制代码
# 1. letter 生成A4纸张尺寸
from reportlab.lib.pagesizes import letter
print(letter)    # (612.0, 792.0)  X轴大小,Y轴大小

# 2. TTFont 设置字体
from reportlab.pdfbase import pdfmetrics        # 注册字体方法
from reportlab.pdfbase.ttfonts import TTFont	# 获取字体类
pdfmetrics.registerFont(TTFont('song', 'simsun.ttc'))

# 3. reportlab自带的颜色库
from reportlab.lib import colors

# 4. Canvas画布,相当于工作区
from reportlab.pdfgen import canvas
canvas.Canvas('新PDF文件名称.pdf', pagesize=letter)  # letter=A4纸大小

# 5. 设置字体
c = canvas.Canvas(filename, pagesize=letter)
c.setFont('song', 12)  # 参数1:已注册字体名称,参数2:字体大小

# 6. TableStyle 给表格设置样式
tab_style = TableStyle([
	('FONTNAME', (0, 0), (-1, 0), 'song-Bold', 12),		 # 表头字体
	('TOPPADDING', (0, 1), (-1, -1), 1),				 # 表头顶部填充
	('BOTTOMPADDING', (0, 0), (-1, 0), 1),				 # 表头底部填充
	('BACKGROUND', (0, 0), (-1, 0), colors.grey),        # 表头背景色
    ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),   # 表头文字颜色
    ('ALIGN', (0, 0), (-1, -1), 'CENTER'),               # 单元格文本居中
    ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),              # 单元格文本垂直居中
    ("FONT", (0, 0), (-1, -1), 'song', 8),               # 单元格字体
    ('BACKGROUND', (0, 1), (-1, -1), colors.beige),      # 行背景色
    ('BOX', (0, 0), (-1, -1), 0.5, colors.black),        # 表格边框颜色
    ('INNERGRID', (0, 0), (-1, -1), 0.5, colors.black),  # 表格单元格颜色
])
# ('FONTNAME', (0,     0),     (-1,    0),     'song-Bold', 12)
#   样式名称,   (开始列,开始行), (结束列, 结束行), 字体,         字体大小)   对应关系

# 7. Table 绘制表格
# Table(数据,style=样式,colWidths=[列宽],rowHeights=[行高])
table_data= [
	('姓名', '性别', '年龄', '民族'),
	('张三', '男', 20, '汉'),
	('李四', '男', 21, '汉'),
	('王小小', '女', 18, '汉'),
]
colWidths = [20, 15, 13, 15]
rowHeights = [29]*len(table_data)
x = 30
y = 600
table1 = Table(table_data, style=table_style, colWidths=colWidths, rowHeights=rowHeights)
table1.wrapOn(self, letter[0], letter[1]) 
table1.drawOn(self, x, y)  # 起始点(x,y)

# 8. 添加图片
filepath = 'd:\images\tttt.png'
image = ImageReader(filepath)
c.drawImage(image, x, y, width=120, height=60)  
# 起始点(x,y) width=图片宽度,height=高度
 
# 9. 添加文本
c.drawString(x, y, text) # 起始点(x,y)

# 10. 垂直写入内容的 语法糖
def translate(func):
    def wrapper(*args, **kwargs):
        ox = letter[0] / 2
        oy = letter[1] / 2
        args[0].saveState()
        args[0].translate(ox, oy)   # 将中心点设置为工作区原点
        args[0].rotate(90)			# 然后按照原点顺时针旋转90度
        func(args[0], *args[1:], **kwargs)
        args[0].restoreState()
    return wrapper

三. 扩展canvas类

python 复制代码
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import Table, TableStyle
from reportlab.lib import colors
from reportlab.lib.utils import ImageReader

# 水平写入扩展
class PDFCanvas(canvas.Canvas):
    FONTS_DIR = r"D:\fonts"
    font_path = os.path.join(FONTS_DIR, 'simsun.ttc')
    pdfmetrics.registerFont(TTFont('song', font_path))

    def __init__(self, filename, **kwargs):
        super().__init__(filename, **kwargs)

    def draw_image(self, filename, **kwargs):
        IMAGES_DIR = r'C:\下载图片'
        filepath = os.path.join(IMAGES_DIR, filename)
        x = kwargs.get('x', 20)                                 # x轴位置
        y = kwargs.get('y', 756)                                # y轴位置
        width = kwargs.get('width', 48)                         # 图片宽度
        height = kwargs.get('height', 18)
        image = ImageReader(filepath)
        self.drawImage(image, x, y, width=width, height=height)

    def draw_text(self, text, **kwargs):
        x = kwargs.get('x', None)
        y = kwargs.get('y', None)
        font = kwargs.get('font', 'song')
        font_size = kwargs.get('font_size', 12)
        self.setFont(font, font_size)
        text_width = self.stringWidth(text, font, font_size)
        x = x if x else (letter[0] - text_width) / 2
        self.drawString(x, y, text)

    def draw_table(self, table_data, **kwargs):
        title = kwargs.get('title', '')
        if title:
            table_data = [(title,), *table_data]
        x = kwargs.get('x', 30)  # 表格X轴位置
        y = kwargs.get('y', 650)  # 表格Y轴位置
        colWidths = kwargs.get('colWidths', 120)
        table_style = kwargs.get('table_style', tab_style)  # 表格样式
        table_h = Table(table_data, style=table_style, colWidths=colWidths, rowHeights=[29]*len(table_data))
        table_h.wrapOn(self, letter[0], letter[1])
        table_h.drawOn(self, x, y)


# 垂直写入扩展
def translate(func):
    def wrapper(*args, **kwargs):
        ox = letter[0] / 2
        oy = letter[1] / 2
        args[0].saveState()
        args[0].translate(ox, oy)   # 将中心点设置为工作区原点
        args[0].rotate(90)			# 然后按照原点顺时针旋转90度
        func(args[0], *args[1:], **kwargs)
        args[0].restoreState()
    return wrapper


class PDFCanvas(canvas.Canvas):

    font_path = os.path.join(FONTS_DIR, 'simsun.ttc')
    pdfmetrics.registerFont(TTFont('song', font_path))

    def __init__(self, filename, **kwargs):
        super().__init__(filename, **kwargs)

    @translate
    def draw_image(self, filename, **kwargs):
    	IMAGES_DIR = r'C:\下载图片'
        filepath = os.path.join(IMAGES_DIR, filename)
        x = kwargs.get('x', 20)                                 # x轴位置
        y = kwargs.get('y', 756)                                # y轴位置
        width = kwargs.get('width', 48)                         # 图片宽度
        height = kwargs.get('height', 18)                       # 图像路径
        x1 = -(letter[1] / 2 - y)
        y = letter[0] / 2 - x
        image = ImageReader(filepath)
        self.drawImage(image, x1, y, width=width, height=height)

    @translate
    def draw_text(self, text, **kwargs):
        x = kwargs.get('x', 20)
        y = kwargs.get('y', None)
        font = kwargs.get('font', 'song')
        font_size = kwargs.get('font_size', 12)
        self.setFont(font, font_size)
        text_width = self.stringWidth(text, font, font_size)
        x1 = -(letter[1] / 2 - y) if y else -int(text_width / 2)
        y = letter[0] / 2 - x
        self.drawString(x1, y, text)

    @translate
    def draw_table(self, table_data, **kwargs):
        title = kwargs.get('title', '')
        if title:
            table_data = [(title,), *table_data]
        x = kwargs.get('x', 30)  # 表格X轴位置
        y = kwargs.get('y', 650)  # 表格Y轴位置
        colWidths = kwargs.get('colWidths', 120)
        table_style = kwargs.get('table_style', tab_style)  # 表格样式
        x1 = -(letter[1] / 2 - y)
        y = letter[0] / 2 - x
        table1 = Table(table_data, style=table_style, colWidths=colWidths, rowHeights=[29]*len(table_data))
        table1.wrapOn(self, letter[0], letter[1])
        table1.drawOn(self, x1, y)

四. 水平写入完整代码

python 复制代码
import os
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import Table, TableStyle
from reportlab.lib import colors
from reportlab.lib.utils import ImageReader

# 水平写入内容
tab_style = TableStyle([
	('FONTNAME', (0, 0), (-1, 0), 'song-Bold', 12),		 # 表头字体
	('TOPPADDING', (0, 1), (-1, -1), 1),				 # 表头顶部填充
	('BOTTOMPADDING', (0, 0), (-1, 0), 1),				 # 表头底部填充
	('BACKGROUND', (0, 0), (-1, 0), colors.grey),        # 表头背景色
    ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),   # 表头文字颜色
    ('ALIGN', (0, 0), (-1, -1), 'CENTER'),               # 单元格文本居中
    ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),              # 单元格文本垂直居中
    ("FONT", (0, 0), (-1, -1), 'song', 8),               # 单元格字体
    ('BACKGROUND', (0, 1), (-1, -1), colors.beige),      # 行背景色
    ('BOX', (0, 0), (-1, -1), 0.5, colors.black),        # 表格边框颜色
    ('INNERGRID', (0, 0), (-1, -1), 0.5, colors.black),  # 表格单元格颜色
])

class PDFCanvas(canvas.Canvas):
    FONTS_DIR = r"D:\fonts"
    font_path = os.path.join(FONTS_DIR, 'simsun.ttc')
    pdfmetrics.registerFont(TTFont('song', font_path))

    def __init__(self, filename, **kwargs):
        super().__init__(filename, **kwargs)

    def draw_image(self, filename, **kwargs):
        IMAGES_DIR = r'C:\下载图片'
        filepath = os.path.join(IMAGES_DIR, filename)
        x = kwargs.get('x', 20)                                 # x轴位置
        y = kwargs.get('y', 756)                                # y轴位置
        width = kwargs.get('width', 48)                         # 图片宽度
        height = kwargs.get('height', 18)
        image = ImageReader(filepath)
        self.drawImage(image, x, y, width=width, height=height)

    def draw_text(self, text, **kwargs):
        x = kwargs.get('x', None)
        y = kwargs.get('y', None)
        font = kwargs.get('font', 'song')
        font_size = kwargs.get('font_size', 12)
        self.setFont(font, font_size)
        text_width = self.stringWidth(text, font, font_size)
        x = x if x else (letter[0] - text_width) / 2
        self.drawString(x, y, text)

    def draw_table(self, table_data, **kwargs):
        title = kwargs.get('title', '')
        if title:
            table_data = [(title,), *table_data]
        x = kwargs.get('x', 30)  # 表格X轴位置
        y = kwargs.get('y', 650)  # 表格Y轴位置
        colWidths = kwargs.get('colWidths', 120)
        table_style = kwargs.get('table_style', tab_style)  # 表格样式
        table_h = Table(table_data, style=table_style, colWidths=colWidths, rowHeights=[29]*len(table_data))
        table_h.wrapOn(self, letter[0], letter[1])
        table_h.drawOn(self, x, y)


if __name__ == '__main__':
    c = PDFCanvas('水平写入.pdf', pagesize=letter)
    image_name = '123.png'
    c.draw_image(image_name, x=70, y=400, width=320, height=240)
    text1 = '我现在需要回答用户关于"reportlab letter"的问题'
    c.draw_text(text1,  y=700, font_size=16)
    table1 = [
        ('姓名', '性别', '年龄', '民族'),
        ('张三', '男', 20, '汉'),
        ('李四', '男', 21, '汉'),
        ('王小小', '女', 18, '汉'),
    ]

    c.draw_table(table1, x=100, y=200, colWidths=[80, 80, 80, 80])
    c.save()

五. 垂直写入完整代码

python 复制代码
import os
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import Table, TableStyle
from reportlab.lib import colors
from reportlab.lib.utils import ImageReader


# 垂直写入内容
def translate(func):
    def wrapper(*args, **kwargs):
        ox = letter[0] / 2
        oy = letter[1] / 2
        args[0].saveState()
        args[0].translate(ox, oy)   # 将中心点设置为工作区原点
        args[0].rotate(90)			# 然后按照原点顺时针旋转90度
        func(args[0], *args[1:], **kwargs)
        args[0].restoreState()
    return wrapper
    
tab_style = TableStyle([
	('FONTNAME', (0, 0), (-1, 0), 'song-Bold', 12),		 # 表头字体
	('TOPPADDING', (0, 1), (-1, -1), 1),				 # 表头顶部填充
	('BOTTOMPADDING', (0, 0), (-1, 0), 1),				 # 表头底部填充
	('BACKGROUND', (0, 0), (-1, 0), colors.grey),        # 表头背景色
    ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),   # 表头文字颜色
    ('ALIGN', (0, 0), (-1, -1), 'CENTER'),               # 单元格文本居中
    ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),              # 单元格文本垂直居中
    ("FONT", (0, 0), (-1, -1), 'song', 8),               # 单元格字体
    ('BACKGROUND', (0, 1), (-1, -1), colors.beige),      # 行背景色
    ('BOX', (0, 0), (-1, -1), 0.5, colors.black),        # 表格边框颜色
    ('INNERGRID', (0, 0), (-1, -1), 0.5, colors.black),  # 表格单元格颜色
])


class PDFCanvas(canvas.Canvas):
    FONTS_DIR = r"D:\fonts"
    font_path = os.path.join(FONTS_DIR, 'simsun.ttc')
    pdfmetrics.registerFont(TTFont('song', font_path))

    def __init__(self, filename, **kwargs):
        super().__init__(filename, **kwargs)

    @translate
    def draw_image(self, filename, **kwargs):
        IMAGES_DIR = r'C:\下载图片'
        filepath = os.path.join(IMAGES_DIR, filename)
        x = kwargs.get('x', 20)                                 # x轴位置
        y = kwargs.get('y', 756)                                # y轴位置
        width = kwargs.get('width', 48)                         # 图片宽度
        height = kwargs.get('height', 18)                       # 图像路径
        x1 = -(letter[1] / 2 - y)
        y = letter[0] / 2 - x
        image = ImageReader(filepath)
        self.drawImage(image, x1, y, width=width, height=height)

    @translate
    def draw_text(self, text, **kwargs):
        x = kwargs.get('x', 20)
        y = kwargs.get('y', None)
        font = kwargs.get('font', 'song')
        font_size = kwargs.get('font_size', 12)
        self.setFont(font, font_size)
        text_width = self.stringWidth(text, font, font_size)
        x1 = -(letter[1] / 2 - y) if y else -int(text_width / 2)
        y = letter[0] / 2 - x
        self.drawString(x1, y, text)

    @translate
    def draw_table(self, table_data, **kwargs):
        title = kwargs.get('title', '')
        if title:
            table_data = [(title,), *table_data]
        x = kwargs.get('x', 30)  # 表格X轴位置
        y = kwargs.get('y', 650)  # 表格Y轴位置
        colWidths = kwargs.get('colWidths', 120)
        table_style = kwargs.get('table_style', tab_style)  # 表格样式
        x1 = -(letter[1] / 2 - y)
        y = letter[0] / 2 - x
        table1 = Table(table_data, style=table_style, colWidths=colWidths, rowHeights=[29]*len(table_data))
        table1.wrapOn(self, letter[0], letter[1])
        table1.drawOn(self, x1, y)


if __name__ == '__main__':
    c = PDFCanvas('垂直写入.pdf', pagesize=letter)
    image_name = '123.png'
    c.draw_image(image_name, x=280, y=100, width=320, height=200)
    text1 = '我现在需要回答用户关于"reportlab letter"的问题'
    c.draw_text(text1, x=50, font_size=16)
    table1 = [
        ('姓名', '性别', '年龄', '民族'),
        ('张三', '男', 20, '汉'),
        ('李四', '男', 21, '汉'),
        ('王小小', '女', 18, '汉'),
    ]

    c.draw_table(table1, x=400, y=100, colWidths=[80, 80, 80, 80])
    c.save()
相关推荐
qq3332129724(中职生)15 分钟前
python推箱子游戏
python
hu_yuchen36 分钟前
如何使用PyCharm自动化测试
ide·python·pycharm
FL16238631291 小时前
[python]通过whl文件安装pyheif安装教程和简单使用案例
开发语言·python
wapicn991 小时前
查看手机在线状态,保障设备安全运行
java·网络·数据库·python·php
Andy__M2 小时前
【AI入门】MCP 初探
人工智能·python·mac
北京_宏哥2 小时前
🔥PC端自动化测试实战教程-4-pywinauto 操作PC端应用程序窗口 - 上篇(详细教程)
前端·python·测试
北京_宏哥2 小时前
🔥PC端自动化测试实战教程-3-pywinauto 启动PC端应用程序 - 下篇(详细教程)
前端·windows·python
浪淘沙jkp2 小时前
智慧水务项目(八)基于Django 5.1 版本PyScada详细安装实战
后端·python·django·pyscada
互联网搬砖老肖2 小时前
21 天 Python 计划:MySQL索引机制从基础到应用
python·mysql·adb
Lounger662 小时前
107.二叉树的层序遍历II- 力扣(LeetCode)
python·算法·leetcode