一、說明
二、需要哪些程序包?
????????首先,您只需要一個 Python 環(huán)境,最好是 3.10 或更高版本。本教程中的代碼是在使用 Python 3.10.12 的 Google Colab 環(huán)境中執(zhí)行的。
????????第一步是確保在 Python 環(huán)境中安裝以下包:
- img2pdf
- PyPDF2
- Pillow
????????Pip 可用于在 Colab 中安裝這些軟件包:
!pip install img2pdf PyPDF2 Pillow
????????第一個包img2pdf將用于將圖像轉(zhuǎn)換為PDF文件。然后,PyPDF2 可用于將多個 PDF 合并為一個 PDF 文件。枕頭是一個圖像處理庫;它提供了轉(zhuǎn)換所需的附加功能。
????????現(xiàn)在可以導(dǎo)入這些包以及 和 。os
google.colab
# required libraries
import os
import img2pdf
import PyPDF2
from PIL import Image
from google.colab import files
三、img2pdf官方文檔
img2pdf是一個開源的Python包,用于將圖像轉(zhuǎn)換為pdf格式。它包括另一個模塊枕頭,也可用于增強(qiáng)圖像(亮度,對比度和其他東西) 使用此命令安裝軟件包
pip install img2pdf
??以下是實現(xiàn):圖像可以使用img2pdf模塊提供的img2pdf.convert()函數(shù)轉(zhuǎn)換為pdf字節(jié),然后在wb模式下打開pdf文件并用字節(jié)寫入。
- 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
四、準(zhǔn)備映像
????????在編寫更多代碼之前,了解每個圖像的文件位置非常重要。為了盡可能簡化此操作,可以在 Colab 環(huán)境中創(chuàng)建一個新文件夾:
!mkdir images
????????所有圖像都需要使用 提供的上傳程序同時上傳到此位置。這些文件將根據(jù)其名稱進(jìn)行排序,因此它們應(yīng)命名為類似 .google.colab
page1.png, page2.png, ..., page9.png
os.chdir("images")
files.upload()
????????將圖像存儲在已知的文件位置后,其名稱可以存儲在列表中。
imgs = os.listdir()
imgs.sort()
????????如果圖像超過 9 個,則此方法可能會出現(xiàn)問題,應(yīng)按文件所需的順序創(chuàng)建列表。
五、將圖像轉(zhuǎn)換為 PDF
????????然后可以使用 for 循環(huán)遍歷每個圖像,將其轉(zhuǎn)換為 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")
六、合并文檔
????????將圖像轉(zhuǎn)換為 PDF 文件后,可以獨立使用并使用 下載它們,也可以將它們合并在一起。要將文件合并在一起,請?zhí)崛?PDF 文件列表并按頁碼對其進(jìn)行排序。files.download('filename.pdf')
os.chdir("../pdfs")
pdfs = os.listdir()
????????同樣,如果有超過 9 個圖像或 PDF,它們應(yīng)按各自的順序存儲在列表中。
????????對象可用于將每個 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將按其各自名稱的順序包含每個圖像。
七、完整程序
????????完整的代碼可以在下面找到。它是高度可定制的,以滿足大多數(shù)用例。文章來源:http://www.zghlxwxcb.cn/news/detail-691932.html
!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
-
亨特·菲利普斯文章來源地址http://www.zghlxwxcb.cn/news/detail-691932.html
·
到了這里,關(guān)于如何在 Python 中將圖像轉(zhuǎn)換為 PDF的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!