解決 Windows 上的 pdftotext 安裝問題

依照 Google 「AI 摘要」建議的 pdftotext 安裝方法。安裝後要執行的時候,發現 Python 的 pdftotext 套件,還需要額外手動安裝 ImageMagickGhostscriptTesseract。雖然三個相依軟體都有安裝,但是 Ghostscript 的設定似乎出了問題,導致 pdftotext 無法使用。

Photo by Bob Richards on StockSnap


問題描述:

使用 `pip install pdftotext` 安裝 Python 套件後,執行 `pdftotext` 時顯示錯誤訊息:

'C:\Program' 不是內部或外部命令、可執行的程式或批次檔。

Tesseract Not Found! Please Install it ...

Imagemagick Not Found!

Please install it...

原因分析:

`pip install pdftotext` 安裝的是 Python 包裝套件,並非真正的 Xpdf 工具。包裝套件相依於 ImageMagick、Ghostscript 和 Tesseract 共三個軟體,如果相依套件安裝或沒有正確設定,會導致產 `C:\Python310\Scripts\pdftotext.exe` 無法直接使用。

解決步驟

步驟 1:開啟命令提示字元 (CMD),檢查目前的 pdftotext 位置

where pdftotext

輸出結果:

C:\Python310\Scripts\pdftotext.exe


步驟 2:嘗試解除安裝 Python 套件

pip uninstall pdftotext

輸出結果:

WARNING: Skipping pdftotext as it is not installed.

代表套件沒有正確安裝,執行解除安裝後,pdftotext.exe 執行檔仍然存在。


步驟 3:手動刪除錯誤的執行檔

del C:\Python310\Scripts\pdftotext.exe


步驟 4:確認已移除

where pdftotext

輸出結果:

INFO: Could not find files for the given pattern(s).

步驟 5:Windows 建議安裝 pdftotext 的方法:

(1) 從 https://www.xpdfreader.com/download.html 下載「Xpdf 命令列工具」(如果需要圖形介面的軟體,可以改安裝 XpdfReader)

(2) 將「Xpdf 命令列工具」解壓縮到 C:\xpdf (可以將安裝位置,改成自己偏好的硬碟路徑)

(3) 參考 Windows 11 環境變數及 Path 設定,將 C:\xpdf\bin64 加入到 Windows 的 PATH 環境變數中

(4) 重新載入 PATH 環境變數:

輸入 refreshenv 指令,或關閉目前的命令提示字元視窗,然後開啟新視窗。

refreshenv

輸出結果:

Refreshing environment variables from registry for cmd.exe. Please wait...Finished..

(5) 驗證是否成功安裝

where pdftotext

預期輸出:

C:\xpdf\bin64\pdftotext.exe

或者輸入

pdftotext -v

預期結果

pdftotext version 4.05 [www.xpdfreader.com] ...

代表已經順利安裝 pdftotext



留言