Python nổi tiếng thư viện package lớn. Với thư viện, chuyển PDF sang CSV. CSV là tập dữ liệu hàng/cột. Có package chuyển PDF sang CSV nhưng dùng Tabula-py module. Phần lớn tabula-py viết Java đọc PDF chuyển DataFrame Python thành JSON.
Để dùng tabula-py, cần Java cài sẵn. Chuyển PDF sang CSV:
- Cài gói: pip install tabula-py.
- Đọc file read_pdf("file location", pages=number) trả DataFrame.
- Chuyển DataFrame sang Excel: tabula.convert_into('pdf-filename', 'name_this_file.csv',output_format="csv", pages="all") xuất PDF ra Excel.
Ví dụ
Dùng IPL Match Schedule Document chuyển sang Excel.
# Import 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)
Kết quả
Chạy mã chuyển PDF thành file Excel (CSV).
