Hướng dẫn python read table from pdf - bảng đọc python từ pdf

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 unplash

Tì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

  1. Cài đặt một thư viện Tabula-Py.
  2. Nhập thư viện.
  3. Đọc một tệp PDF.
  4. Đọc một bảng trên một trang cụ thể của tệp pdf.
  5. Đọc nhiều bảng trên cùng một trang của tệp PDF.
  6. 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 Ganesh
file1 = "https://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 Ganesh

4. Đọ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 Ganesh
file2 = "https://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 Ganesh
To 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 = "https://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])
print(tables[1])
Image bởi Satya Ganeshimage của Satya Ganesh

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ùngTênNghề nghiệp
1 DavidQuản lý sản phẩm
2 Sư TửQuản trị viên IT
3 JohnLuậ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
2
import tabula
3
import tabula
4

import tabula
1
import tabula
6
import tabula
3
import tabula
8

import tabula
9
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
0
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
1
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
2223
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
0
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
5
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
6

file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
7
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
8

Output:

Hướng dẫn python read table from pdf - bảng đọc python từ pdf

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
0

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ảngpoints towards the desired table of a given index

Tệp PDF được sử dụng ở đây là PDF.

Hướng dẫn python read table from pdf - bảng đọc python từ pdf

Python3

import tabula
3
file2 = "https://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])
0

file2 = "https://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])
1
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
0
file2 = "https://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])
3
file2 = "https://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])
4
file2 = "https://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

file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
7
file2 = "https://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])
7
file2 = "https://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])
8
file2 = "https://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])
9

Output:


Làm cách nào để trích xuất một bảng từ hình ảnh PDF trong Python?

Tôi sẽ đề nghị bạn trích xuất bảng bằng cách sử dụng tabula ...
Sử dụng Tesseract để phát hiện vòng quay và hình ảnh Mogrify để sửa nó ..
Sử dụng OpenCV để tìm và trích xuất bảng ..
Sử dụng OpenCV để tìm và trích xuất từng ô từ bảng ..

Tôi có thể trích xuất các bảng từ pdf không?

Làm thế nào tôi có thể trích xuất bảng từ tài liệu PDF? Trước tiên, bạn cần thêm một tệp để trích xuất: Kéo và thả tệp PDF của bạn hoặc nhấp vào bên trong khu vực màu trắng để chọn tệp và chọn Định dạng đầu ra. Sau đó nhấp vào nút 'Trích xuất'. Khi trích xuất bảng của tài liệu PDF hoàn tất, bạn có thể tải xuống các tệp kết quả của mình.drag & drop your PDF file or click inside the white area for choose a file and select output format. Then click the 'EXTRACT' button. When PDF document's table extraction is completed, you can download your result files.

Làm cách nào để lấy dữ liệu từ PDF trong Python?

Bây giờ, chúng tôi tạo một đối tượng của lớp trang PYPDF2.Đối tượng đầu đọc pdf có chức năng getPage () lấy số trang (chỉ mục biểu mẫu bắt đầu 0) làm đối số và trả về đối tượng trang.Đối tượng trang có chức năng trích xuất () để trích xuất văn bản từ trang PDF.Cuối cùng, chúng tôi đóng đối tượng tệp PDF.

Chúng ta có thể đọc dữ liệu từ PDF trong Python không?

Có một vài thư viện Python sử dụng mà bạn có thể trích xuất dữ liệu từ PDFS.Ví dụ: bạn có thể sử dụng thư viện PYPDF2 để trích xuất văn bản từ các tệp PDF trong đó văn bản theo cách tuần tự hoặc được định dạng, tức là trong các dòng hoặc biểu mẫu.Bạn cũng có thể trích xuất các bảng trong các tệp PDF thông qua thư viện Camelot.. For example, you can use the PyPDF2 library for extracting text from PDFs where text is in a sequential or formatted manner i.e. in lines or forms. You can also extract tables in PDFs through the Camelot library.