Trích xuất dữ liệu từ các bảng trong PDF - Sử dụng một dòng duy nhất trong Python
Trong bài viết này, bạn sẽ tìm hiểu cách tốt nhất để trích xuất và xuất dữ liệu từ các bảng trong các tệp PDF sang CSV/Excel bằng Python.
Ảnh của David Clode trên unplashTìm nạp các bảng từ các tệp PDF không còn là một nhiệm vụ khó khăn nữa, bạn có thể thực hiện việc này bằng một dòng duy nhất trong Python.
Những gì bạn sẽ học
- Cài đặt một thư viện Tabula-Py.
- Nhập thư viện.
- Đọc một tệp PDF.
- Đọc một bảng trên một trang cụ thể của tệp pdf.
- Đọc nhiều bảng trên cùng một trang của tệp PDF.
- Chuyển đổi tệp PDF trực tiếp sang tệp CSV.
Tabula
Tabula là một trong những gói hữu ích không chỉ cho phép bạn xóa các bảng từ các tệp PDF mà còn chuyển đổi tệp PDF trực tiếp thành tệp CSV.
Vậy hãy bắt đầu…
1. Cài đặt thư viện Tabula-Py
pip install tabula-py
2. Nhập thư viện Tabula
import tabula
3. Đọc tệp PDF
Cho phép loại bỏ bản PDF này vào khung dữ liệu PANDAS.PDF into pandas Data Frame.
Hình ảnh của Satya Ganeshfile1 = "//nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf[file1,pages=1]table[0]
Hãy xem đầu ra của đoạn mã trên được thực hiện trong Google Colabs
Hình ảnh của Satya Ganesh4. Đọc một bảng trên một trang cụ thể của tệp PDF.
Giả sử chúng ta cần loại bỏ tệp PDF này chứa nhiều trang trong đó.PDF FILE which contains multiple pages in it.
Hình ảnh của Satya Ganeshimage bởi Satya Ganeshfile2 = "//nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file
table1 = tabula.read_pdf[file2 ,pages=1]# To read tables in secord page of PDF file
table2 = tabula.read_pdf[file2 ,pages=2]print[table1[0]]
print[table2[0]]
5. Điều gì xảy ra nếu có nhiều bảng trên cùng một trang của tệp PDF?
Giả sử chúng ta cần cạo 2 bảng này trên cùng một trang của tệp PDF. PDF file.
Hình ảnh của Satya GaneshTo read multiple tables we need to add extra parametermultiple_tables = True -> Read multiple tables as independent tables
multiple_tables = False -> Read multiple tables as single table
5.1. Đọc nhiều bảng là bảng độc lập
file3 = "//nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data3.pdf"tables = tabula.read_pdf[file3 ,pages=1, multiple_tables=True]print[tables[0]]Image bởi Satya Ganeshimage của Satya Ganesh
print[tables[1]]
5.2 Đọc nhiều bảng dưới dạng một bảng
tables = tabula.read_pdf[file3 ,pages=1,multiple_tables=False]tables[0]Image bởi Satya Ganesh
6. Covert một tệp PDF trực tiếp vào tệp CSV
Chúng tôi có thể trực tiếp chuyển đổi tệp PDF chứa dữ liệu bảng trực tiếp sang tệp CSV bằng phương thức Convert_into [] trong Thư viện Tabula.convert_into[] method in tabula library.
1. Chuyển đổi bảng trong 1 trang của tệp PDF thành CSV
# output just the first page tables in the PDF to a CSVtabula.convert_into["pdf_file_name", "Name_of_csv_file.csv"]
2. Chuyển đổi tất cả bảng trong tệp PDF thành CSV
tabula.convert_into["pdf_file_name","Name_of_csv_file.csv",all = True]
Sự kết luận
Tôi hy vọng bạn đã học được một cách tuyệt vời để cạo các bảng tệp PDF bằng một dòng duy nhất trong Python.
Kiểm tra các bài viết liên quan của tôi
Tài liệu tham khảo
Cảm ơn vì đã đọc 😃 Chúc một ngày tốt lành
Chủ đề này là về cách trích xuất các bảng từ PDF Enter Python. Lúc đầu, hãy để thảo luận về những gì mà một tệp PDF?
PDF [định dạng tài liệu di động] có thể là một định dạng tệp đã nắm bắt được tất cả thời tiết của một tài liệu in dưới dạng bitmap mà bạn chỉ có thể xem, điều hướng, in hoặc chuyển tiếp cho người khác. Các tệp PDF được tạo bằng Adobe Acrobat,
Thí dụ :
Giả sử một tệp pdf chứa một bảng
Tên người dùng | Tên | Nghề nghiệp |
1 | David | Quản lý sản phẩm |
2 | Sư Tử | Quản trị viên IT |
3 | John | Luật sư |
Và chúng tôi muốn đọc bảng này vào chương trình Python của chúng tôi. Vấn đề này có thể được giải quyết bằng cách sử dụng một số phương pháp. Hãy để thảo luận từng người một.
Phương pháp 1: Sử dụng Tabula-Py
Tabula-Py là một trình bao bọc Python đơn giản của Tabula-Java, có thể đọc các bảng trong PDF. Bạn có thể cài đặt thư viện Tabula-Py bằng lệnh.
pip install tabula-py pip install tabulate
Các phương pháp được sử dụng trong ví dụ là:
read_pdf []: Đọc dữ liệu từ các bảng của tệp PDF của địa chỉ đã cho reads the data from the tables of the PDF file of the given address
Tabulation []: Sắp xếp dữ liệu theo định dạng bảng arranges the data in a table format
Tệp PDF được sử dụng ở đây là PDF.
Python3
import tabula
1 import tabula
2import tabula
3 import tabula
4import tabula
1 import tabula
6import tabula
3 import tabula
8import tabula
9file1 = "//nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf[file1,pages=1]table[0]0
file1 = "//nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf[file1,pages=1]table[0]1
file1 = "//nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf[file1,pages=1]table[0]2223
file1 = "//nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf[file1,pages=1]table[0]0
file1 = "//nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf[file1,pages=1]table[0]5
file1 = "//nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf[file1,pages=1]table[0]6
file1 = "//nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf[file1,pages=1]table[0]7
file1 = "//nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf[file1,pages=1]table[0]8
Output:
Phương pháp 2: Sử dụng Camelot
Camelot là một thư viện Python giúp trích xuất các bảng từ các tệp PDF. Bạn có thể cài đặt thư viện Camelot-Py bằng lệnh
import tabula
0Các phương pháp được sử dụng trong ví dụ là:
read_pdf []: Đọc dữ liệu từ các bảng của tệp PDF của địa chỉ đã cho reads the data from the tables of the pdf file of the given address
Bảng [INDEX] .DF: Điểm hướng về bảng mong muốn của một chỉ mục nhất địnhpoints towards the desired table of a given index
Tệp PDF được sử dụng ở đây là PDF.
Python3
import tabula
3 file2 = "//nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file0
table1 = tabula.read_pdf[file2 ,pages=1]# To read tables in secord page of PDF file
table2 = tabula.read_pdf[file2 ,pages=2]print[table1[0]]
print[table2[0]]
file2 = "//nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file1
table1 = tabula.read_pdf[file2 ,pages=1]# To read tables in secord page of PDF file
table2 = tabula.read_pdf[file2 ,pages=2]print[table1[0]]
print[table2[0]]
file1 = "//nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf[file1,pages=1]table[0]0
file2 = "//nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file3
table1 = tabula.read_pdf[file2 ,pages=1]# To read tables in secord page of PDF file
table2 = tabula.read_pdf[file2 ,pages=2]print[table1[0]]
print[table2[0]]
file2 = "//nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file4
table1 = tabula.read_pdf[file2 ,pages=1]# To read tables in secord page of PDF file
table2 = tabula.read_pdf[file2 ,pages=2]print[table1[0]]
print[table2[0]]
file2 = "//nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file5
table1 = tabula.read_pdf[file2 ,pages=1]# To read tables in secord page of PDF file
table2 = tabula.read_pdf[file2 ,pages=2]print[table1[0]]
print[table2[0]]
file1 = "//nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf[file1,pages=1]table[0]7
file2 = "//nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file7
table1 = tabula.read_pdf[file2 ,pages=1]# To read tables in secord page of PDF file
table2 = tabula.read_pdf[file2 ,pages=2]print[table1[0]]
print[table2[0]]
file2 = "//nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file8
table1 = tabula.read_pdf[file2 ,pages=1]# To read tables in secord page of PDF file
table2 = tabula.read_pdf[file2 ,pages=2]print[table1[0]]
print[table2[0]]
file2 = "//nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file9
table1 = tabula.read_pdf[file2 ,pages=1]# To read tables in secord page of PDF file
table2 = tabula.read_pdf[file2 ,pages=2]print[table1[0]]
print[table2[0]]
Output: