Python มีไลบรารีมากมายสำหรับจัดการไฟล์ บทความนี้แสดงวิธีแปลง PDF เป็น Excel (CSV) ด้วยโมดูล Tabula-py ซึ่งส่วนใหญ่เขียนด้วย Java เพื่ออ่าน PDF และแปลงเป็น DataFrame/JSON
ข้อกำหนด: ต้องติดตั้ง Java ในระบบก่อน
ขั้นตอน
- ติดตั้งแพ็กเกจ:
pip install tabula-py - อ่านไฟล์ PDF ด้วย read_pdf("file location", pages=number) → คืนค่า DataFrame
- แปลงเป็น CSV/Excel ด้วย tabula.convert_into('pdf-filename', 'output.csv', output_format="csv", pages="all")
ตัวอย่างโค้ด
# 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 เป็นไฟล์ CSV (Excel)
