PYTHON PDF 工具 如何在 Python 中将 PDF 转换为图像 Curtis Chau 更新日期:6月 22, 2025 Download IronPDF pip 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article PDF(可攜式文件格式)是互聯網上傳輸數據最受歡迎的文件格式,因為它能保留內容格式並通過安全權限幫助保護數據。 在某些情況下,我們需要將 PDF 文件轉換為 JPG 圖像或任何其他圖像格式,如 PNG、BMP、TIFF 或 GIF。 有很多在線資源可用於 JPG 轉換,但如果我們能夠用 Python 創建自己的 PDF 到圖像轉換工具,那將會很酷。 什麼是 Python? Python 是一種用於構建軟體應用、網站、自動化任務、數據分析以及執行人工智能及機器學習任務的高級編程語言。 由於它是解釋型語言,因此也是腳本語言,這使其在快速開發和測試方面更加強大。 要創建 PDF 到圖像轉換器,我們需要在電腦上安裝 Python 3+。 從官方網站下載並安裝最新版本。 在這篇文章中,我們將使用 Python 的 PDF 到圖像庫創建我們自己的圖像轉換應用。 為此,我們將使用 Python 的兩個最受歡迎的庫:PDF2Image 和 PyMuPDF。 如何在 Python 中將 PDF 文件轉換為圖像文件 安裝將 PDF 轉換為圖像的 Python 庫。 從任意位置加載現有的 PDF 文件。 使用轉換方法。 遍歷文件的頁面。 使用保存方法將每頁保存為 JPG 或 PNG 圖像。 創建新的 Python 文件 打開 Python IDLE 應用並按下 Ctrl + N 鍵。 文字編輯器將會開啟。 你可以使用你喜歡的文字編輯器。 將文件保存為 pdf2image.py,保存位置與你想轉換為圖像的 PDF 文件相同。 我們將要使用的輸入 PDF 文件包含 28 頁,具體如下: 使用 PDF2Image 庫將 PDF 文件轉換為圖像文件 1. 安裝 PDF2Image Python 庫 PDF2Image 是一個包裝 pdftocairo 和 pdftoppm 的模塊。 它在 Python 3.7+ 上運行,以將 PDF 轉換為 PIL 圖像對象。 其之前的發行歷史表明它只包裝 pdftoppm 進行 PDF 到圖像的轉換,並且僅在 Python 3+ 上工作。 要安裝 pdf2image 包,打開你的 Windows 命令提示符或 Windows PowerShell,並使用以下 pip 命令: pip install pdf2image pip install pdf2image SHELL Pip(首選安裝程序)是 Python 的包管理器。 它下載並安裝第三方軟件包,這些包提供 Python 標準庫中沒有的功能和特性。 注意:若要從命令行的任何位置執行該命令,Python 必須添加到 PATH。 對於 Python 3+,建議使用 pip3,因為它是 pip 的更新版本。 2. 安裝 Poppler Poppler 是一個用於處理 PDF 文件的免費和開源庫。 它被用來呈現 PDF 文件,讀取內容,並修改 PDF 文件中的內容。 它通常被 Linux 用戶使用。 但是,對於 Windows,我們需要下載最新版本的 Poppler。 對於 Windows Windows 用戶可以在這裡下載最新版本的 Poppler:@oschwartz10612 version。 然後你需要將 bin/文件夾添加到 PATH 環境變量。 對於 Mac Mac 用戶也必須安裝Poppler。 可以使用Brew安裝: brew install poppler brew install poppler SHELL 對於 Linux 大多數 Linux 發行版都附帶 pdftoppm 和 pdftocairo 命令行實用程序。 如果這些實用程序尚未安裝,您可以使用包管理器安裝 poppler-utils。 對於平台無關(使用 conda) 安裝 poppler: conda install -c conda-forge poppler conda install -c conda-forge poppler SHELL 安裝 pdf2image: pip install pdf2image pip install pdf2image SHELL 現在一切準備就緒,讓我們開始編寫代碼來將 PDF 轉換為圖像。 3. 用於將 PDF 文件轉換為圖像文件的代碼 以下代碼將執行輸入 PDF 文件的圖像轉換: from pdf2image import convert_from_path # Notify the user that the process is starting print("Please wait while the file is being loaded.") file = convert_from_path('file.pdf') # Iterate over all pages in the PDF file for i in range(len(file)): # Update user on progress print("Progress: " + str(round(i / len(file) * 100)) + "%") # Save each page as a JPG image file file[i].save('page' + str(i + 1) + '.jpg', 'JPEG') # Notify the user that the conversion is successful print("Conversion Successful") from pdf2image import convert_from_path # Notify the user that the process is starting print("Please wait while the file is being loaded.") file = convert_from_path('file.pdf') # Iterate over all pages in the PDF file for i in range(len(file)): # Update user on progress print("Progress: " + str(round(i / len(file) * 100)) + "%") # Save each page as a JPG image file file[i].save('page' + str(i + 1) + '.jpg', 'JPEG') # Notify the user that the conversion is successful print("Conversion Successful") PYTHON 在上述代碼中,我們首先使用 convert_from_path 方法打開文件。 該方法打開位於指定路徑的文件。 然後,我們循環遍歷要轉換為 JPG 圖像的 PDF 文件的每一頁。 最後,使用 save 方法將每個轉換的頁面保存為 JPG 圖像文件。現在,執行程序並等待轉換完成。 輸出圖像文件保存在與程序相同的文件夾中。 使用 PyMuPDF 庫將 PDF 文件轉換為圖像 1. 安裝 PyMuPDF Python 庫 PyMuPDF 是 MuPDF 的擴展 Python 綁定,MuPDF 是一個輕量級電子書、PDF 和 XPS 閱讀器、渲染器和工具包。 它可以用於將 PDF 轉換為其他格式,如 JPG 或 PNG。 PyMuPDF 運行在 Python 3.7+ 版本。 要安裝 PyMuPDF 包,打開你的 Windows 命令提示符或 Windows PowerShell,並使用以下 pip 命令: pip install pymupdf pip install pymupdf SHELL 注意,PyMuPDF 不需要任何額外的庫,就像 PDF2Image 包一樣。 2. 用於將 PDF 文件轉換為圖像的代碼 以下代碼將從 PyMuPDF 導入 fitz 模塊,以便我們將 PDF 轉換為圖像: import fitz # PyMuPDF # Open the PDF file doc = fitz.open("file.pdf") # Iterate over each page in the document for x in range(len(doc)): page = doc.load_page(x) # Load a specific page pix = page.get_pixmap() # Render page to image output = "output/pdfpage" + str(x + 1) + ".png" # Specify output path pix.save(output) # Save the image to the output path # Close the document doc.close() import fitz # PyMuPDF # Open the PDF file doc = fitz.open("file.pdf") # Iterate over each page in the document for x in range(len(doc)): page = doc.load_page(x) # Load a specific page pix = page.get_pixmap() # Render page to image output = "output/pdfpage" + str(x + 1) + ".png" # Specify output path pix.save(output) # Save the image to the output path # Close the document doc.close() PYTHON 在上述代碼中,文件名作為參數傳遞給 fitz.open 方法來打開文件。接下來,我遍歷整個文檔並分別加載每頁。 get_pixmap 方法用於將每個文檔頁面轉換為圖像像素,並使用 save 方法將生成的圖像保存在輸出文件夾中。 最後,打開的文檔將被關閉以釋放內存。 與 PDF2Image 相比,PyMuPDF 在將 PDF 轉換為 PNG 時更快。 由於其壓縮比,PDF2Image 可以在 PNG 格式的情況下較慢。 輸出與 PDF2Image 相同: 用 C# 渲染 PDF 到圖像的轉換 IronPDF 庫 IronPDF 是一個用於生成、閱讀和操作 PDF 文件的庫。 它的專長在於通過 Chromium 引擎將 HTML 渲染為 PDF。這一功能使其在需要將 HTML 文件或 URL 轉換為 PDF 文檔的開發人員中受歡迎。 此外,它提供將多種格式轉換為 PDF 文件的功能。 您也可以使用簡單的兩行代碼將 PDF 光柵化為圖像。 以下代碼演示了如何將 PDF 轉換為不同的圖像格式: using IronPdf; var Renderer = new IronPdf.ChromePdfRenderer(); var PDF = Renderer.RenderUrlAsPdf("https://example.com"); PDF.SaveAs("html.pdf"); // Rasterize the PDF List<string> Images = PDF.RasterizeToImageFiles(ImageType.Png); using IronPdf; var Renderer = new IronPdf.ChromePdfRenderer(); var PDF = Renderer.RenderUrlAsPdf("https://example.com"); PDF.SaveAs("html.pdf"); // Rasterize the PDF List<string> Images = PDF.RasterizeToImageFiles(ImageType.Png); Imports IronPdf Private Renderer = New IronPdf.ChromePdfRenderer() Private PDF = Renderer.RenderUrlAsPdf("https://example.com") PDF.SaveAs("html.pdf") ' Rasterize the PDF Dim Images As List(Of String) = PDF.RasterizeToImageFiles(ImageType.Png) $vbLabelText $csharpLabel Download IronPDF and try it for 免費使用。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新日期 6月 22, 2025 在 Python 的列表中查找項目 本文探索各種方法,當使用Python查找列表中的任何元素時,將為您提供對可用選項和其應用的全面理解。 閱讀更多 更新日期 6月 22, 2025 Spyder Python IDE:完整指南 在本文中,我們將探索什麼是 Spyder,如何安裝它,以及如何使用其關鍵功能。 閱讀更多 更新日期 7月 28, 2025 用 Pytest 寫測試在 Python 中 PyTest 是一個强大、灵活且用户友好的测试框架,在 Python 社区中获得了极大的普及 閱讀更多 如何在 Python 中將 HTML 轉換為 PDF