Hướng dẫn how to read data from pdf file in python - cách đọc dữ liệu từ tệp pdf trong python

Tất cả các bạn phải quen thuộc với PDFS là gì. Trên thực tế, chúng là một trong những phương tiện kỹ thuật số quan trọng và được sử dụng rộng rãi nhất. & NBSP; PDF là viết tắt của định dạng tài liệu di động. Nó sử dụng tiện ích mở rộng .pdf. Nó được sử dụng để trình bày và trao đổi tài liệu một cách đáng tin cậy, độc lập với phần mềm, phần cứng hoặc hệ điều hành. Được Adobe, PDF hiện là một tiêu chuẩn mở được duy trì bởi Tổ chức Tiêu chuẩn hóa Quốc tế (ISO). PDF có thể chứa các liên kết và nút, trường mẫu, âm thanh, video và logic kinh doanh. Trong bài viết này, chúng ta sẽ tìm hiểu, làm thế nào chúng ta có thể thực hiện các hoạt động khác nhau như: & nbsp; & nbsp;Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system.
Invented by Adobe, PDF is now an open standard maintained by the International Organization for Standardization (ISO). PDFs can contain links and buttons, form fields, audio, video, and business logic.
In this article, we will learn, how we can do various operations like: 
 

  • Trích xuất văn bản từ PDF
  • Xoay trang PDF
  • Hợp nhất pdfs
  • Tách PDF
  • Thêm hình mờ vào các trang PDF

Sử dụng các tập lệnh Python đơn giản! Cài đặt & nbsp; Chúng tôi sẽ sử dụng mô-đun của bên thứ ba, PYPDF2.PYPDF2 là một thư viện Python được xây dựng dưới dạng bộ công cụ PDF. Nó có khả năng: & nbsp; & nbsp;
Installation 
We will be using a third-party module, PyPDF2.
PyPDF2 is a python library built as a PDF toolkit. It is capable of: 
 

  • Trích xuất thông tin tài liệu (Tiêu đề, Tác giả, Mạnh)
  • Chia tài liệu trang theo từng trang
  • Hợp nhất trang tài liệu theo từng trang
  • Trang cắt xén
  • Hợp nhất nhiều trang vào một trang
  • Mã hóa và giải mã các tệp PDF
  • và nhiều hơn nữa!

Để cài đặt PYPDF2, hãy chạy lệnh sau từ dòng lệnh: & nbsp; & nbsp;
 

 pip3 install PyPDF2

Tên mô-đun này nhạy cảm với trường hợp, vì vậy hãy đảm bảo Y là chữ thường và mọi thứ khác là chữ hoa. Tất cả các tệp mã và PDF được sử dụng trong hướng dẫn/bài viết này đều có sẵn tại đây.1. Trích xuất văn bản từ tệp pdf & nbsp; & nbsp;y is lowercase and everything else is uppercase. All the code and PDF files used in this tutorial/article are available here.
1. Extracting text from PDF file 
 

Python

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
6
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
7

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
8
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
pdfFileObj = open('example.pdf', 'rb')
0
pdfFileObj = open('example.pdf', 'rb')
1
pdfFileObj = open('example.pdf', 'rb')
2223
pdfFileObj = open('example.pdf', 'rb')
4
pdfFileObj = open('example.pdf', 'rb')
5

pdfFileObj = open('example.pdf', 'rb')
6
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
pdfFileObj = open('example.pdf', 'rb')
8

pdfFileObj = open('example.pdf', 'rb')
9
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
0

pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
1
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
3
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
4
pdfFileObj = open('example.pdf', 'rb')
5

pdfFileObj = open('example.pdf', 'rb')
9
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
7

pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
8

Đầu ra của chương trình trên trông như thế này: & nbsp; & nbsp;
 

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]

Hãy để chúng tôi cố gắng hiểu mã trên trong các khối: & nbsp; & nbsp;
 

pdfFileObj = open('example.pdf', 'rb')
  • Chúng tôi đã mở ví dụ.pdf ở chế độ nhị phân. & Nbsp; và lưu đối tượng tệp dưới dạng pdffiLeobj. & Nbsp;example.pdf in binary mode. And saved the file object as pdfFileObj.
     
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
  • Ở đây, chúng tôi tạo một đối tượng của lớp PDFFileReader của mô -đun PYPDF2 và vượt qua đối tượng tệp PDF và nhận đối tượng đầu đọc PDF. & NBSP;PdfFileReader class of PyPDF2 module and pass the PDF file object & get a PDF reader object.
     
print(pdfReader.numPages)
  • Thuộc tính Numpages cung cấp số lượng trang trong tệp PDF. Ví dụ: trong trường hợp của chúng tôi, nó là 20 (xem dòng đầu ra đầu tiên). & Nbsp;property gives the number of pages in the PDF file. For example, in our case, it is 20 (see first line of output).
     
pageObj = pdfReader.getPage(0)
  • 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 (bắt đầu từ chỉ mục 0) làm đối số và trả về đối tượng trang. & Nbsp;PageObject class of PyPDF2 module. PDF reader object has function getPage() which takes page number (starting from index 0) as argument and returns the page object.
     
print(pageObj.extractText())
  • Đối tượng trang có chức năng trích xuất () để trích xuất văn bản từ trang PDF. & Nbsp;extractText() to extract text from the PDF page.
     
pdfFileObj.close()
  • Cuối cùng, chúng tôi đóng đối tượng tệp PDF.

Lưu ý: Mặc dù các tệp PDF rất tuyệt vời để đưa ra văn bản theo cách mà mọi người dễ in và đọc, nhưng chúng không đơn giản đối với phần mềm để phân tích thành bản rõ. Như vậy, PYPDF2 có thể mắc lỗi khi trích xuất văn bản từ PDF và thậm chí có thể không thể mở một số tệp PDF. Thật không may, đó là rất nhiều bạn có thể làm về điều này, thật không may. PYPDF2 có thể chỉ đơn giản là không thể làm việc với một số tệp PDF cụ thể của bạn. While PDF files are great for laying out text in a way that’s easy for people to print and read, they’re not straightforward for software to parse into plaintext. As such, PyPDF2 might make mistakes when extracting text from a PDF and may even be unable to open some PDFs at all. It isn’t much you can do about this, unfortunately. PyPDF2 may simply be unable to work with some of your particular PDF files.

2. Xoay PDF Trang & NBSP;
 

Python

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
6
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
7

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
8
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
pdfFileObj = open('example.pdf', 'rb')
0
pdfFileObj = open('example.pdf', 'rb')
1
pdfFileObj = open('example.pdf', 'rb')
2223
pdfFileObj = open('example.pdf', 'rb')
4
pdfFileObj = open('example.pdf', 'rb')
5

pdfFileObj = open('example.pdf', 'rb')
6
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
pdfFileObj = open('example.pdf', 'rb')
8

pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
1
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
3
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
4
pdfFileObj = open('example.pdf', 'rb')
5

Đầu ra của chương trình trên trông như thế này: & nbsp; & nbsp;

Hãy để chúng tôi cố gắng hiểu mã trên trong các khối: & nbsp; & nbsp;

Chúng tôi đã mở ví dụ.pdf ở chế độ nhị phân. & Nbsp; và lưu đối tượng tệp dưới dạng pdffiLeobj. & Nbsp;

print(pageObj.extractText())
4
print(pageObj.extractText())
9

print(pageObj.extractText())
4
pdfFileObj.close()
1

Ở đây, chúng tôi tạo một đối tượng của lớp PDFFileReader của mô -đun PYPDF2 và vượt qua đối tượng tệp PDF và nhận đối tượng đầu đọc PDF. & NBSP;

print(pdfReader.numPages)
3
pdfWriter = PyPDF2.PdfFileWriter()
0

print(pdfReader.numPages)
3
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
8

print(pdfReader.numPages)
3
pdfWriter = PyPDF2.PdfFileWriter()
4

Thuộc tính Numpages cung cấp số lượng trang trong tệp PDF. Ví dụ: trong trường hợp của chúng tôi, nó là 20 (xem dòng đầu ra đầu tiên). & Nbsp;

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 (bắt đầu từ chỉ mục 0) làm đối số và trả về đối tượng trang. & Nbsp;

Đối tượng trang có chức năng trích xuất () để trích xuất văn bản từ trang PDF. & Nbsp;

Cuối cùng, chúng tôi đóng đối tượng tệp PDF.

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
00

Lưu ý: Mặc dù các tệp PDF rất tuyệt vời để đưa ra văn bản theo cách mà mọi người dễ in và đọc, nhưng chúng không đơn giản đối với phần mềm để phân tích thành bản rõ. Như vậy, PYPDF2 có thể mắc lỗi khi trích xuất văn bản từ PDF và thậm chí có thể không thể mở một số tệp PDF. Thật không may, đó là rất nhiều bạn có thể làm về điều này, thật không may. PYPDF2 có thể chỉ đơn giản là không thể làm việc với một số tệp PDF cụ thể của bạn.

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
08

2. Xoay PDF Trang & NBSP;rotated_example.pdf looks like ( right image) after rotation:
 

Hướng dẫn how to read data from pdf file in python - cách đọc dữ liệu từ tệp pdf trong python

print(pdfReader.numPages)
1
print(pdfReader.numPages)
2
 

  • print(pdfReader.numPages)
    3
    20
    PythonBasics
    S.R.Doty
    August27,2008
    Contents
    
    1Preliminaries
    4
    1.1WhatisPython?...................................
    ..4
    1.2Installationanddocumentation....................
    .........4 [and some more lines...]
    8
    20
    PythonBasics
    S.R.Doty
    August27,2008
    Contents
    
    1Preliminaries
    4
    1.1WhatisPython?...................................
    ..4
    1.2Installationanddocumentation....................
    .........4 [and some more lines...]
    9
    pdfFileObj = open('example.pdf', 'rb')
    0
    print(pdfReader.numPages)
    7
    pdfFileObj = open('example.pdf', 'rb')
    4
    pdfFileObj = open('example.pdf', 'rb')
    5
pdfWriter = PyPDF2.PdfFileWriter()
  • print(pdfReader.numPages)
    3
    pdfFileObj = open('example.pdf', 'rb')
    6
    20
    PythonBasics
    S.R.Doty
    August27,2008
    Contents
    
    1Preliminaries
    4
    1.1WhatisPython?...................................
    ..4
    1.2Installationanddocumentation....................
    .........4 [and some more lines...]
    9
    pdfFileObj = open('example.pdf', 'rb')
    8PdfFileWriter class of PyPDF2 module.
for page in range(pdfReader.numPages):
        pageObj = pdfReader.getPage(page)
        pageObj.rotateClockwise(rotation)
        pdfWriter.addPage(pageObj)
  • print(pdfReader.numPages)
    3
    pageObj = pdfReader.getPage(0)
    55
    20
    PythonBasics
    S.R.Doty
    August27,2008
    Contents
    
    1Preliminaries
    4
    1.1WhatisPython?...................................
    ..4
    1.2Installationanddocumentation....................
    .........4 [and some more lines...]
    9
    pageObj = pdfReader.getPage(0)
    7getPage() method of PDF reader class. Now, we rotate the page by rotateClockwise() method of page object class. Then, we add a page to PDF writer object using addPage() method of PDF writer class by passing the rotated page object.
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
0
  • print(pdfReader.numPages)
    3
    pageObj = pdfReader.getPage(0)
    9
    print(pageObj.extractText())
    0
    print(pageObj.extractText())
    1
    print(pageObj.extractText())
    2
    print(pageObj.extractText())
    3write() method of PDF writer object. Finally, we close the original PDF file object and the new file object.

3. Hợp nhất các tệp PDF & NBSP;
 

Python

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
6
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
7

print(pdfReader.numPages)
1
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
12

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
14
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
16

print(pdfReader.numPages)
3
pageObj = pdfReader.getPage(0)
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
19
print(pageObj.extractText())
1
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
21

print(pageObj.extractText())
4
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
23

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
25
pdfFileObj = open('example.pdf', 'rb')
0
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
27
pdfFileObj.close()
7
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
29

print(pageObj.extractText())
4
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
31

print(pdfReader.numPages)
1
pdfWriter = PyPDF2.PdfFileWriter()
6

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
35
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
37
pdfFileObj = open('example.pdf', 'rb')
2223
for page in range(pdfReader.numPages):
        pageObj = pdfReader.getPage(page)
        pageObj.rotateClockwise(rotation)
        pdfWriter.addPage(pageObj)
4
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
41

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
43
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
45

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
47
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
49
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
51

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
01
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
02
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
05
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
06

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
08

Đầu ra của chương trình trên là PDF kết hợp, kết hợp_example.pdf, thu được bằng cách hợp nhất ví dụ.pdf và rotated_example.pdf. & Nbsp;combined_example.pdf,obtained by merging example.pdf and rotated_example.pdf.
 

  • Chúng ta hãy xem xét các khía cạnh quan trọng của chương trình này: & nbsp; & nbsp; & nbsp;
      
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
1
  • Để hợp nhất, chúng tôi sử dụng lớp được xây dựng sẵn, pdffilemerger của mô-đun pypdf2.PdfFileMerger of PyPDF2 module. 
    Here, we create an object pdfMerger of PDF merger class
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
2
  • Bây giờ, chúng tôi nối thêm đối tượng tệp của mỗi đối tượng PDF vào PDF bằng phương thức append ().append() method.
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
3
  • Cuối cùng, chúng tôi viết các trang PDF vào tệp PDF đầu ra bằng phương thức ghi của đối tượng sáp nhập PDF.write method of PDF merger object.

4. Tách tệp PDF & NBSP;
 

Python

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
6
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
7

print(pdfReader.numPages)
1
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
12

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
14
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
16

print(pdfReader.numPages)
3
pageObj = pdfReader.getPage(0)
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
19
print(pageObj.extractText())
1
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
21

print(pdfReader.numPages)
1
pdfWriter = PyPDF2.PdfFileWriter()
6

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
35
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
37
pdfFileObj = open('example.pdf', 'rb')
2223
for page in range(pdfReader.numPages):
        pageObj = pdfReader.getPage(page)
        pageObj.rotateClockwise(rotation)
        pdfWriter.addPage(pageObj)
4
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
41

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
43
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
45

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
01
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
02
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
05
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
06

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
08

Đầu ra của chương trình trên là PDF kết hợp, kết hợp_example.pdf, thu được bằng cách hợp nhất ví dụ.pdf và rotated_example.pdf. & Nbsp;

pdfFileObj = open('example.pdf', 'rb')
19
pdfFileObj = open('example.pdf', 'rb')
20

Chúng ta hãy xem xét các khía cạnh quan trọng của chương trình này: & nbsp; & nbsp; & nbsp;

pdfFileObj = open('example.pdf', 'rb')
19
pdfFileObj = open('example.pdf', 'rb')
28

Để hợp nhất, chúng tôi sử dụng lớp được xây dựng sẵn, pdffilemerger của mô-đun pypdf2.

print(pageObj.extractText())
4
pdfFileObj = open('example.pdf', 'rb')
34
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
06

Bây giờ, chúng tôi nối thêm đối tượng tệp của mỗi đối tượng PDF vào PDF bằng phương thức append ().

Cuối cùng, chúng tôi viết các trang PDF vào tệp PDF đầu ra bằng phương thức ghi của đối tượng sáp nhập PDF.

4. Tách tệp PDF & NBSP;

print(pdfReader.numPages)
3
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
8

print(pdfReader.numPages)
1
pdfWriter = PyPDF2.PdfFileWriter()
6

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
35
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
37
pdfFileObj = open('example.pdf', 'rb')
2223
for page in range(pdfReader.numPages):
        pageObj = pdfReader.getPage(page)
        pageObj.rotateClockwise(rotation)
        pdfWriter.addPage(pageObj)
4
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
41

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
43
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
45

print(pdfReader.numPages)
3
pdfFileObj = open('example.pdf', 'rb')
67

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
01
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
02
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
05
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
06

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
08

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
08split 1 (page 0,1), split 2(page 2,3), split 3(page 4-end).
No new function or class has been used in the above python program. Using simple logic and iterations, we created the splits of passed PDF according to the passed list splits.
5. Adding watermark to PDF pages
 

Python

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
6
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
7

Đầu ra của chương trình trên là PDF kết hợp, kết hợp_example.pdf, thu được bằng cách hợp nhất ví dụ.pdf và rotated_example.pdf. & Nbsp;

Chúng ta hãy xem xét các khía cạnh quan trọng của chương trình này: & nbsp; & nbsp; & nbsp;

Để hợp nhất, chúng tôi sử dụng lớp được xây dựng sẵn, pdffilemerger của mô-đun pypdf2.

print(pdfReader.numPages)
3
pdfFileObj = open('example.pdf', 'rb')
92
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
4
pdfFileObj = open('example.pdf', 'rb')
94

print(pdfReader.numPages)
3
pdfFileObj = open('example.pdf', 'rb')
96

Bây giờ, chúng tôi nối thêm đối tượng tệp của mỗi đối tượng PDF vào PDF bằng phương thức append ().

print(pdfReader.numPages)
1
pdfWriter = PyPDF2.PdfFileWriter()
6

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
35
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
37
pdfFileObj = open('example.pdf', 'rb')
2223
for page in range(pdfReader.numPages):
        pageObj = pdfReader.getPage(page)
        pageObj.rotateClockwise(rotation)
        pdfWriter.addPage(pageObj)
4
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
41

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
43
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
45

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
01
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
02
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
05
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
06

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
08

print(pdfReader.numPages)
3
pageObj = pdfReader.getPage(0)
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
19
print(pageObj.extractText())
1
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
21

print(pdfReader.numPages)
1
pdfWriter = PyPDF2.PdfFileWriter()
6

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
35
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
37
pdfFileObj = open('example.pdf', 'rb')
2223
for page in range(pdfReader.numPages):
        pageObj = pdfReader.getPage(page)
        pageObj.rotateClockwise(rotation)
        pdfWriter.addPage(pageObj)
4
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
41

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
43
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
45

print(pageObj.extractText())
4
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
40

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
01
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
02
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
05
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
06

print(pdfReader.numPages)
3
pdfWriter = PyPDF2.PdfFileWriter()
0

print(pdfReader.numPages)
3
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
8

print(pdfReader.numPages)
3
pdfWriter = PyPDF2.PdfFileWriter()
4

20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
01
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
02
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
9
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
05
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
06

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
08

print(pdfReader.numPages)
3
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
08
 

Hướng dẫn how to read data from pdf file in python - cách đọc dữ liệu từ tệp pdf trong python

  • Đầu ra của chương trình trên là PDF kết hợp, kết hợp_example.pdf, thu được bằng cách hợp nhất ví dụ.pdf và rotated_example.pdf. & Nbsp;
     
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
4
  • Chúng ta hãy xem xét các khía cạnh quan trọng của chương trình này: & nbsp; & nbsp; & nbsp;add_watermark() function.
  • Để hợp nhất, chúng tôi sử dụng lớp được xây dựng sẵn, pdffilemerger của mô-đun pypdf2.add_watermark() function: 
     
20
PythonBasics
S.R.Doty
August27,2008
Contents

1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
5
  • Bây giờ, chúng tôi nối thêm đối tượng tệp của mỗi đối tượng PDF vào PDF bằng phương thức append ().watermark.pdf. To the passed page object, we use mergePage() function and pass the page object of the first page of the watermark PDF reader object. This will overlay the watermark over the passed page object.

Cuối cùng, chúng tôi viết các trang PDF vào tệp PDF đầu ra bằng phương thức ghi của đối tượng sáp nhập PDF.
Now, you can easily create your own PDF manager!
References: 
 

  • https://automatetheboringstuff.com/chapter13/
  • https://pythonhosted.org/PyPDF2/

4. Tách tệp PDF & NBSP;Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
 


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

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.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.

Làm cách nào để đọc nội dung của PDF trong Python?

Hãy để chúng tôi cố gắng hiểu mã trên trong các khối:..
pdffileObj = open ('example.pdf', 'rb') Chúng tôi đã mở ví dụ.....
pdfreader = pypdf2.pdffilereader (pdffileObj) ....
In (pdfreader.numpages) ....
pageObj = pdfreader.getPage (0) ....
In (pageObj.exTractText ()) ....
pdfFileObj.close().

Làm cách nào để đọc dữ liệu từ PDF?

Trích xuất dữ liệu từ PDF đến Excel..
Mở một tờ Excel ..
Tab dữ liệu> Nhận thả xuống Dữ liệu> Từ Tệp> Từ PDF ..
Chọn tệp PDF của bạn và nhấp vào Nhập ..
Bây giờ bạn sẽ thấy một ngăn Navigator hiển thị các bảng & trang trong tệp PDF của bạn cùng với bản xem trước ..
Chọn một bảng và nhấp vào Tải ..

Làm cách nào để trích xuất văn bản cụ thể từ PDF trong Python?

Bước 1: Nhập tất cả các thư viện.Bước 2: Chuyển đổi tệp PDF thành định dạng TXT và đọc dữ liệu.Bước 3: Sử dụng chức năng .Findall () của các biểu thức thông thường để trích xuất các từ khóa.Use “. findall()” function of regular expressions to extract keywords.