如何使用 Python 將 PDF 檔案轉換為 Excel 檔案?
Python 擁有大量的庫來處理不同型別的操作。透過這篇文章,我們將瞭解如何將 pdf 檔案轉換為 Excel 檔案。Python 中有各種可用的包可以將 pdf 轉換為 CSV,但我們將使用 **tabula-py** 模組。**tabula-py** 的大部分是用 Java 編寫的,它讀取 pdf 文件並將 Python DataFrame 轉換為 JSON 物件。
為了使用 **tabula-py**,我們必須在系統中預先安裝 Java。現在,要將 pdf 檔案轉換為 csv,我們將按照以下步驟操作:
首先,在命令列中鍵入 **pip install tabula-py** 來安裝所需的包。
現在使用 read_pdf("檔案位置", pages=頁碼) 函式讀取檔案。這將返回 DataFrame。
使用 tabula.convert_into(‘pdf檔名’, ‘要儲存的檔名.csv’,output_format= "csv", pages= "all") 將 DataFrame 轉換為 Excel 檔案。它通常將 pdf 檔案匯出為 excel 檔案。
示例
在這個例子中,我們使用了 **IPL 比賽日程文件** 並將其轉換為 excel 檔案。
# Import the required Module import tabula # Read a PDF File df = tabula.read_pdf("IPLmatch.pdf", pages='all')[0] # convert PDF into CSV tabula.convert_into("IPLmatch.pdf", "iplmatch.csv", output_format="csv", pages='all') print(df)
輸出
執行上述程式碼將把 pdf 檔案轉換為 excel (csv) 檔案。
廣告