中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

黑龍江電商網(wǎng)站建設(shè)廣州seo工程師

黑龍江電商網(wǎng)站建設(shè),廣州seo工程師,易企秀可以做微網(wǎng)站嗎,廣告公司創(chuàng)意廣告語一、說明 如何使得圖像轉(zhuǎn)化成pdf文件, 想要將一個或多個圖像轉(zhuǎn)換為 PDF 文檔?看看img2pdf和PyPDF2軟件包就是您的最佳選擇。 二、需要哪些程序包? 首先,您只需要一個 Python 環(huán)境,最好是 3.10 或更高版本。本教程中的代…

一、說明

如何使得圖像轉(zhuǎn)化成pdf文件, 想要將一個或多個圖像轉(zhuǎn)換為 PDF 文檔?看看img2pdfPyPDF2軟件包就是您的最佳選擇。

二、需要哪些程序包?

????????首先,您只需要一個 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)入這些包以及 和 。osgoogle.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.colabpage1.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 imagewith Image.open(img) as image: # convert the image to a PDFpdf = img2pdf.convert(image.filename)# write the PDF to its final destinationwith 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 pdfwith open(pdf, 'rb') as pdfFile:# merge each filepdfMerge.append(PyPDF2.PdfReader(pdfFile))# write the merged pdf 
pdfMerge.write('merged.pdf')# download the final pdf
files.download('merged.pdf')

最終合并的PDF將按其各自名稱的順序包含每個圖像。

七、完整程序

????????完整的代碼可以在下面找到。它是高度可定制的,以滿足大多數(shù)用例。

!pip install img2pdf PyPDF2 Pillow
!mkdir images
# required libraries
import os
import img2pdf
import PyPDF2
from PIL import Image
from google.colab import filesos.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 imagewith Image.open(img) as image: # convert the image to a PDFpdf = img2pdf.convert(image.filename)# write the PDF to its final destinationwith 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 pdfwith open(pdf, 'rb') as pdfFile:# merge each filepdfMerge.append(PyPDF2.PdfReader(pdfFile))# write the merged pdf 
pdfMerge.write('merged.pdf')# download the final pdf
files.download('merged.pdf')

八、引用

  1. https://www.geeksforgeeks.org/python-convert-image-to-pdf-using-img2pdf-module/
  2. Merging PDFs with Python | Python-bloggers
  3. 亨特·菲利普斯

    ·
http://www.risenshineclean.com/news/57193.html

相關(guān)文章:

  • 杭州濱江網(wǎng)站建設(shè)公司整合營銷是什么
  • 河南手機(jī)網(wǎng)站建設(shè)公司哪家好seo網(wǎng)站關(guān)鍵詞排名軟件
  • 月嫂云商城網(wǎng)站建設(shè)百度廣告聯(lián)盟下載
  • 揚州市江都區(qū)城鄉(xiāng)建設(shè)局網(wǎng)站愛站網(wǎng)關(guān)鍵詞排名
  • 中英版網(wǎng)站系統(tǒng)企業(yè)網(wǎng)絡(luò)推廣計劃書
  • 上海期貨配資網(wǎng)站開發(fā)上google必須翻墻嗎
  • 服務(wù)網(wǎng)絡(luò)營銷的含義太原seo推廣
  • 如何做跨境電商新手入門教程seo排名如何
  • 外包客服公司好做嗎首頁排名seo
  • 珠海市網(wǎng)站開發(fā)公司seo競價
  • 怎么做火短視頻網(wǎng)站seo門戶網(wǎng)價格是多少錢
  • 一個ip地址上可以做幾個網(wǎng)站今日新聞聯(lián)播
  • 做網(wǎng)站用什么語言要做網(wǎng)絡(luò)推廣
  • 海珠區(qū)有沒有專門做網(wǎng)站的地方全球最大的中文搜索引擎
  • 專業(yè)網(wǎng)站建設(shè) 公司哪家好杭州網(wǎng)站推廣與優(yōu)化
  • 網(wǎng)站推廣中應(yīng)注意哪些事項泉州seo技術(shù)
  • 做網(wǎng)站需要買seo中國是什么
  • 江蘇網(wǎng)站開發(fā)輿情分析網(wǎng)站免費
  • 偷拍網(wǎng)站做色盲測試圖第六版
  • 鄭州百度網(wǎng)站優(yōu)化排名百度賬號申訴
  • 如何建設(shè)網(wǎng)站效果好百度發(fā)布信息的免費平臺
  • 深圳市多語言網(wǎng)站建設(shè)公司廣州百度關(guān)鍵詞排名
  • 人才網(wǎng)站的會計賬如何做網(wǎng)絡(luò)推廣渠道公司
  • 美女做暖暖視頻免費網(wǎng)站網(wǎng)絡(luò)營銷圖片素材
  • 供應(yīng)邯鄲專業(yè)做網(wǎng)站網(wǎng)站seo入門基礎(chǔ)教程書籍
  • 南昌網(wǎng)站建設(shè)哪家好哈爾濱seo推廣
  • 軟件首頁設(shè)計圖微博seo營銷
  • 運營最好的網(wǎng)站西安網(wǎng)站關(guān)鍵詞優(yōu)化推薦
  • 南京網(wǎng)站設(shè)計公司推薦企業(yè)策劃推廣公司
  • 動態(tài)網(wǎng)站開發(fā)教案網(wǎng)頁模版