介绍

PDF 格式是与平台无关,它独立于底层操作系统和渲染引擎。事实上,PDF 是基于一种脚本语言——PostScript,它是第一个独立于设备的页面描述语言。

在本指南中,我们将使用 borb —— 一个专门用于阅读、操作和生成 PDF 文档的 Python 库。它提供了一个低级模型(允许您访问精确的坐标和布局)和一个高级模型(您可以将边距、位置等精确计算委托给布局管理器) .

matplotlib 是一个数据可视化库,也是许多其他流行库(如 Seaborn)背后的引擎。

基于用于创建报告(通常包括图形)的常见 PDF 文档,我们将看看如何使用 borb 将 Matplotlib 图表集成到 PDF 文档中。

安装 borbmatplotlib

borb 可以从 GitHub 上的源代码下载,或通过 pip 安装:

$ pip install borb

matplotlib 也可以通过 pip 安装:

$ pip install matplotlib

用 Borb 在 PDF 文档中集成 Matplotlib 图表

在创建饼图等图表之前,我们将编写一个小的效用函数,该函数生成 N 种颜色,均匀分布在颜色光谱中。

每当我们需要创建绘图并为每个部分着色时,这将对我们有所帮助:

from borb.pdf.canvas.color.color import HSVColor, HexColor
from decimal import Decimal
import typingdef create_n_colors(n: int) -> typing.List[str]:# The base color is borb-bluebase_hsv_color: HSVColor = HSVColor.from_rgb(HexColor("56cbf9"))# This array comprehension creates n HSVColor objects, transforms then to RGB, and then returns their hex stringreturn [HSVColor(base_hsv_color.hue + Decimal(x / 360), Decimal(1), Decimal(1)).to_rgb().to_hex_string() for x in range(0, 360, int(360/n))]

HSL 和 HSV/HSB 是由计算机图形学研究人员在 1970 年代设计的,目的是更接近人类视觉感知色彩属性的方式。在这些模型中,每种色调的颜色都排列在一个径向切片中,围绕中性色的中心轴,范围从底部的黑色到顶部的白色:

用它表示颜色的优点是我们可以轻松地将颜色光谱分成相等的部分。

现在我们可以定义一个 create_pie_chart() 函数(或其他类型图的函数):

# New import(s)
import matplotlib.pyplot as plt
from borb.pdf.canvas.layout.image.chart import Chart
from borb.pdf.canvas.layout.layout_element import Alignmentdef create_piechart(labels: typing.List[str], data: typing.List[float]):# Symetric figure to ensure equal aspect ratiofig1, ax1 = plt.subplots(figsize=(4, 4))ax1.pie(data,explode=[0 for _ in range(0, len(labels))],labels=labels,autopct="%1.1f%%",shadow=True,startangle=90,colors=create_n_colors(len(labels)),)ax1.axis("equal")  # Equal aspect ratio ensures that pie is drawn as a circle.return Chart(plt.gcf(),width=Decimal(200),height=Decimal(200),horizontal_alignment=Alignment.CENTERED,)

在这里,我们使用 Matplotlib 通过 pie() 函数创建饼图。

PyPlot 实例的 gcf() 函数返回当前图形。该图可以嵌入到 PDF 文档中,方法是将其注入到 Chart 构造函数中,并与您的自定义参数(例如width, heighthorizontal_alignment)一起插入。

您只需向Chart构造函数提供一个 Matplotlib 图。

将 Matplotlib 图表添加到 PDF 文档

现在是时候创建我们的 PDF 文档并向其中添加内容了。

# New import(s)
from borb.pdf.document import Document
from borb.pdf.page.page import Page
from borb.pdf.pdf import PDF
from borb.pdf.canvas.layout.page_layout.multi_column_layout import MultiColumnLayout
from borb.pdf.canvas.layout.page_layout.page_layout import PageLayout
from borb.pdf.canvas.layout.text.paragraph import Paragraph# Create empty Document
pdf = Document()# Create empty Page
page = Page()# Add Page to Document
pdf.append_page(page)# Create PageLayout
layout: PageLayout = MultiColumnLayout(page)# Write title
layout.add(Paragraph("About Lorem Ipsum", font_size=Decimal(20), font="Helvetica-Bold"))

我们将在此 PDF 中使用连字符,以确保文本的布局更加流畅。borb 中的连字符非常简单:

# New import(s)
from borb.pdf.canvas.layout.hyphenation.hyphenation import Hyphenation# Create hyphenation algorithm
hyphenation_algorithm: Hyphenation = Hyphenation("en-gb")# Write paragraph
layout.add(Paragraph("""Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.""", text_alignment=Alignment.JUSTIFIED, hyphenation=hyphenation_algorithm))

现在我们可以使用我们之前声明的函数添加饼图;

# Write graph
layout.add(create_piechart(["Loren", "Ipsum", "Dolor"], [0.6, 0.3, 0.1]))

接下来我们将编写另外三个 Paragraph对象。其中一个将不仅仅表示引用(侧面边框,不同字体等)。

# Write paragraph
layout.add(Paragraph("""Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.""", text_alignment=Alignment.JUSTIFIED, hyphenation=hyphenation_algorithm))# Write paragraph
layout.add(Paragraph("""Lorem Ipsum is simply dummy text of the printing and typesetting industry. """, font="Courier-Bold",text_alignment=Alignment.JUSTIFIED, hyphenation=hyphenation_algorithm,border_color=HexColor("56cbf9"),border_width=Decimal(3),border_left=True,padding_left=Decimal(5),padding_bottom=Decimal(5),
))# Write paragraph
layout.add(Paragraph("""Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance.""", text_alignment=Alignment.JUSTIFIED, hyphenation=hyphenation_algorithm))

让我们添加另一个绘图。

# Write graph
layout.add(create_piechart(["Loren", "Ipsum", "Dolor", "Sit", "Amet"], [600, 30, 89, 100, 203]))

还有一段内容(Paragraph):

# Write paragraph
layout.add(Paragraph("""It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).""", text_alignment=Alignment.JUSTIFIED, hyphenation=hyphenation_algorithm))

最后,我们可以存储文档(Document):

# Write to disk
with open("output.pdf", "wb") as pdf_file_handle:PDF.dumps(pdf_file_handle, pdf)

运行此代码会生成如下所示的 PDF 文档:

在本文中,您学习了如何使用 borb 将 Matplotlib 图表集成到 PDF 。

点击下方阅读原文加入社区会员

用 Python 将 matplotlib 图表集成到 PDF 中相关推荐

  1. Python 办公小助手:读取 PDF 中表格并重命名

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 博观而约取,厚积而薄发. 日常工作 ...

  2. 【python批量插入图片到一个pdf中】

    批量插入图片到一个pdf中 需求 解决思路 代码详解 需求 把大量的img图片插入到一个pdf中,一张图片为pdf的一页 解决思路 先把大量的图片转为一个个的pdf,然后合并所有的pdf 话不多说,直 ...

  3. [Python]使用matplotlib库实现在Cell中不完全填充颜色

    效果图镇楼 想看细节的可以一点点看,想Copy代码的,直接看下面代码部分 背景 最近在研究使用matplotlib直接从数据生成表格图片的实现方式. 进阶需求是把其中一列按百分比使背景色从左到右填充单 ...

  4. 使用Python自动遍历并删除扫描PDF中的空白页

    对于经常看扫描PDF资料的人来说,经常会碰到如下问题: 因为一些格式转换的原因,一些空白页时不时的出现,而且规律不定,一会是偶数页码一会是奇数页码,逐个选中删除的话,对于几百页的文档,非常费时. 百度 ...

  5. 解决 win10+pycharm 环境 Matplotlib图表不能在Pycharm中显示的等问题

    解决办法:安装 anaconda Anaconda的优点总结起来就八个字:省时省心.分析利器. 省时省心: Anaconda通过管理工具包.开发环境.Python版本,大大简化了你的工作流程.不仅可以 ...

  6. 解决Matplotlib图表不能在Pycharm中显示的问题:使用Anaconda

    Anaconda的优点总结起来就八个字:省时省心.分析利器. 省时省心: Anaconda通过管理工具包.开发环境.Python版本,大大简化了你的工作流程.不仅可以方便地安装.更新.卸载工具包,而且 ...

  7. python基础——matplotlib——scatter和plot方法中的maker参数(点的样式)

    一.scatter scatter方法主要用来做散点图展示,而plot方法主要用来做折线图展示,也可以用于散点图的展示.两个方法的参数基本是通用的.以scatter方法为例,常用参数包括: plt.s ...

  8. 一行代码让matplotlib图表变高大上

    1 简介 matplotlib作为Python生态中最流行的数据可视化框架,虽然功能非常强大,但默认样式比较简陋,想要制作具有简洁商务风格的图表往往需要编写众多的代码来调整各种参数. 而今天要为大家介 ...

  9. Python使用matplotlib可视化绘制并导出可视化结果图表到PDF文件中

    Python使用matplotlib可视化绘制并导出可视化结果图表到PDF文件中 目录 Python使用matplotlib可视化绘制并导出可视化结果图表到PDF文件中

最新文章

  1. bootstrap 冻结表格,冻结表头
  2. 【每日DP】day6 P1541 乌龟棋(四维DP)难度⭐⭐⭐
  3. spring_快速提示:在Spring中引用其他属性
  4. 网站优化中站点为什么会出现404页面?
  5. linux C库编译
  6. 《17探索,18前行》
  7. PHP函数收藏---不断更新中!
  8. Ubuntu下文件权限管理
  9. 实验一 链式存储结构的基本操作
  10. vmware的vmdk格式虚拟机转换为kvm的qcow2格式
  11. spark读取kafka数据_解决Spark数据倾斜(Data Skew)的N种姿势
  12. linux软盘镜像下载,Linux系统各发行版镜像下载(持续更新)
  13. 阿里内核月报2014年7月-8月
  14. 链家北京二手房交易数据分析
  15. 初唐名臣---凌烟阁上二十四功臣
  16. 纯净版英雄联盟LOL如何开启 2022年8月8日方法
  17. 虚拟化系列-Windows server 2012 Remote桌面与应用
  18. white-space:nowrap normal pre pre-wrap pre-line的区别以及pre和pre-wrap的“首行缩进“问题
  19. 出书最多 [2021年12月 电子学会C语言编程等级考试二级真题解析]
  20. ODL MD-SAL Data Transactions

热门文章

  1. 维修工程师,比你想象的要赚钱
  2. 计算机领域前沿技术总结
  3. LLVM 之父 Chris Lattner:模块化设计决定 AI 前途,不服来辩
  4. 【MySQL基础教程】事务详细介绍
  5. 问题--[__NSCFNumber length]: unrecognized selector sent to instance 0x8b3c310’ - andy_shen
  6. Android_使用Android killer破解apk
  7. Arduino开发之Relay Module
  8. 用 Python 爬取分析每日票房数据
  9. 直播预约|FFA 2022 主会场,11月26日正式上线
  10. 最高检发布破坏计算机信息系统案等六大指导性案例