Python中将图像转换为PDF的方法实现

 更新时间:2023年08月29日 11:06:41   作者:无水先生  
本文主要介绍了Python中将图像转换为PDF的方法实现,主要使用img2pdf和PyPDF2软件包,具有一定的参考价值,感兴趣的可以了解一下

一、说明

如何使得图像转化成pdf文件, 想要将一个或多个图像转换为 PDF 文档?看看img2pdf和PyPDF2软件包就是您的最佳选择。

二、需要哪些程序包?

首先,您只需要一个 Python 环境,最好是 3.10 或更高版本。本教程中的代码是在使用 Python 3.10.12 的 Google Colab 环境中执行的。

 第一步是确保在 Python 环境中安装以下包:

  • img2pdf
  • PyPDF2
  • Pillow

Pip 可用于在 Colab 中安装这些软件包:

pip install img2pdf PyPDF2 Pillow 

第一个包img2pdf将用于将图像转换为PDF文件。然后,PyPDF2 可用于将多个 PDF 合并为一个 PDF 文件。枕头是一个图像处理库;它提供了转换所需的附加功能。

现在可以导入这些包以及 和 。osgoogle.colab

# required libraries
import os
import img2pdf
import PyPDF2
from PIL import Image
from google.colab import files

三、img2pdf官方文档

img2pdf是一个开源的Python包,用于将图像转换为pdf格式。它包括另一个模块枕头,也可用于增强图像(亮度,对比度和其他东西) 使用此命令安装软件包

pip install img2pdf

以下是实现:图像可以使用img2pdf模块提供的img2pdf.convert()函数转换为pdf字节,然后在wb模式下打开pdf文件并用字节写入。

python

# Python3 program to convert image to pdf
# using img2pdf library
# importing necessary libraries
import img2pdf
from PIL import Image
import os
# storing image path
img_path = "C:/Users/Admin/Desktop/GfG_images/do_nawab.png"
# storing pdf path
pdf_path = "C:/Users/Admin/Desktop/GfG_images/file.pdf"
# opening image
image = Image.open(img_path)
# converting into chunks using img2pdf
pdf_bytes = img2pdf.convert(image.filename)
# opening or creating pdf file
file = open(pdf_path, "wb")
# writing pdf files with chunks
file.write(pdf_bytes)
# closing image file
image.close()
# closing pdf file
file.close()
# output
print("Successfully made pdf file")

输出:

Successfully made pdf file

四、准备映像

在编写更多代码之前,了解每个图像的文件位置非常重要。为了尽可能简化此操作,可以在 Colab 环境中创建一个新文件夹:

mkdir images

所有图像都需要使用 提供的上传程序同时上传到此位置。这些文件将根据其名称进行排序,因此它们应命名为类似 .google.colabpage1.png, page2.png, ..., page9.png

os.chdir("images")
files.upload()

将图像存储在已知的文件位置后,其名称可以存储在列表中。

imgs = os.listdir()
imgs.sort()

如果图像超过 9 个,则此方法可能会出现问题,应按文件所需的顺序创建列表。

五、将图像转换为 PDF

然后可以使用 for 循环遍历每个图像,将其转换为 PDF,并将其写入名为 的新文件夹。pdfs

# create a folder called pdfs
os.mkdir("../pdfs")
# loop over each image
for ind, img in enumerate(imgs):
  # open each image
  with Image.open(img) as image: 
    # convert the image to a PDF
    pdf = img2pdf.convert(image.filename)
    # write the PDF to its final destination
    with open(f"../pdfs/pdf{ind+1}.pdf", "wb") as file:
      file.write(pdf)
      print(f"Converted {img} to pdf{ind+1}.pdf")

六、合并文档

将图像转换为 PDF 文件后,可以独立使用并使用 下载它们,也可以将它们合并在一起。要将文件合并在一起,请提取 PDF 文件列表并按页码对其进行排序。files.download('filename.pdf')

os.chdir("../pdfs")
pdfs = os.listdir()

同样,如果有超过 9 个图像或 PDF,它们应按各自的顺序存储在列表中。

对象可用于将每个 PDF 连接成单个文件。PdfMerger

pdfMerge = PyPDF2.PdfMerger()
# loop through each pdf page
for pdf in pdfs:
  # open each pdf
  with open(pdf, 'rb') as pdfFile:
    # merge each file
    pdfMerge.append(PyPDF2.PdfReader(pdfFile))
# write the merged pdf 
pdfMerge.write('merged.pdf')
# download the final pdf
files.download('merged.pdf')

最终合并的PDF将按其各自名称的顺序包含每个图像。

七、完整程序

完整的代码可以在下面找到。它是高度可定制的,以满足大多数用例。

pip install img2pdf PyPDF2 Pillow
mkdir images
# required libraries
import os
import img2pdf
import PyPDF2
from PIL import Image
from google.colab import files
os.chdir("images")
files.upload()
imgs = os.listdir()
# create a folder called pdfs
os.mkdir("../pdfs")
# loop over each image
for ind, img in enumerate(imgs):
  # open each image
  with Image.open(img) as image: 
    # convert the image to a PDF
    pdf = img2pdf.convert(image.filename)
    # write the PDF to its final destination
    with open(f"../pdfs/pdf{ind+1}.pdf", "wb") as file:
      file.write(pdf)
      print(f"Converted {img} to pdf{ind+1}.pdf")
os.chdir("../pdfs")
pdfs = os.listdir()
pdfs.sort()
pdfMerge = PyPDF2.PdfMerger()
# loop through each pdf page
for pdf in pdfs:
  # open each pdf
  with open(pdf, 'rb') as pdfFile:
    # merge each file
    pdfMerge.append(PyPDF2.PdfReader(pdfFile))
# write the merged pdf 
pdfMerge.write('merged.pdf')
# download the final pdf
files.download('merged.pdf')

八、引用

https://www.geeksforgeeks.org/python-convert-image-to-pdf-using-img2pdf-module/
Merging PDFs with Python | Python-bloggers

到此这篇关于Python中将图像转换为PDF的方法实现的文章就介绍到这了,更多相关Python图像转换为PDF内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用Python画股票的K线图的方法步骤

    使用Python画股票的K线图的方法步骤

    这篇文章主要介绍了使用Python画股票的K线图的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • python实现一次创建多级目录的方法

    python实现一次创建多级目录的方法

    这篇文章主要介绍了python实现一次创建多级目录的方法,涉及Python中os模块makedirs方法的使用技巧,非常简单实用,需要的朋友可以参考下
    2015-05-05
  • Python-openpyxl表格读取写入的案例详解

    Python-openpyxl表格读取写入的案例详解

    这篇文章主要介绍了Python-openpyxl表格读取写入的案例分析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • python用于url解码和中文解析的小脚本(python url decoder)

    python用于url解码和中文解析的小脚本(python url decoder)

    这篇文章主要介绍了python用于url解码和中文解析的代码,需要的朋友可以参考下
    2013-08-08
  • wxPython实现绘图小例子

    wxPython实现绘图小例子

    这篇文章主要为大家详细介绍了wxPython实现绘图小例子,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • python中array数组添加一行或一列数据的具体实现

    python中array数组添加一行或一列数据的具体实现

    这篇文章主要给大家介绍了关于python中array数组添加一行或一列数据的具体实现,最近经常使用到数组方法,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-09-09
  • Python模糊查询本地文件夹去除文件后缀的实例(7行代码)

    Python模糊查询本地文件夹去除文件后缀的实例(7行代码)

    下面小编就为大家带来一篇Python模糊查询本地文件夹去除文件后缀的实例(7行代码) 。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • pytorch加载的cifar10数据集过程详解

    pytorch加载的cifar10数据集过程详解

    这篇文章主要介绍了pytorch加载的cifar10数据集,到底有没有经过归一化,本文对这一问题给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2023-11-11
  • Pandas库中ffill函数的具体使用

    Pandas库中ffill函数的具体使用

    ffill(forward fill)是Pandas库中DataFrame和Series对象的一个函数,用于填充缺失值,本文主要介绍了Pandas库中ffill函数的具体使用,具有一定的参考价值,感兴趣的可以了解一下
    2024-07-07
  • 使用C#配合ArcGIS Engine进行地理信息系统开发

    使用C#配合ArcGIS Engine进行地理信息系统开发

    这篇文章主要介绍了使用C#配合ArcGIS Engine进行地理信息系统开发,ArcGIS Engine是Windows系统上可以让程序员创建自定义的GIS桌面程序,需要的朋友可以参考下
    2016-02-02

最新评论