Làm cách nào để xuất sang pdf trong python?

Đôi khi, bạn có thể cần thêm chức năng 'xuất sang pdf' vào ứng dụng hoặc trang web python của mình. Trong bài viết này, chúng ta sẽ xem xét cách xuất dữ liệu sang PDF bằng python. Bạn có thể sử dụng các bước này để xuất HTML sang PDF hoặc thậm chí là văn bản thuần túy sang PDF


Cách tạo tệp PDF bằng Python

Chúng tôi sẽ sử dụng thư viện fpdf để tạo PDF. Nó là viết tắt của pdf miễn phí và thường được sử dụng trong các ứng dụng python để tạo tệp pdf

Bạn có thể cài đặt nó bằng lệnh sau

$ sudo pip install fpdf


1. Tạo tệp PDF từ văn bản

Trong trường hợp này, chúng tôi sẽ nhập thư viện fpdf, thêm một trang, đặt phông chữ của nó và chèn một ô có văn bản của chúng tôi vào đó. Cuối cùng, chúng tôi lưu nó dưới dạng tệp pdf

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   


2. Lưu tệp văn bản dưới dạng PDF

Đôi khi bạn có thể cần chuyển đổi tệp văn bản thành PDF. Bạn cũng có thể làm điều này với fpdf. Trong trường hợp này, chúng tôi sẽ lặp qua tệp văn bản của mình, từng dòng một và thêm từng dòng một vào tệp pdf của chúng tôi, trước khi lưu nó. Trong ví dụ trên, chúng tôi đã thêm hai dòng vào tệp PDF của mình. Đây chỉ là một phần mở rộng của nó. Giả sử bạn muốn lưu dữ liệu tệp văn bản. txt thành dữ liệu. pdf. Đây là một ví dụ để tạo tệp pdf từ nó

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   


3. Chuyển đổi trang web sang PDF

Nếu bạn muốn chuyển đổi trang web/HTML sang PDF thì chúng tôi khuyên bạn nên sử dụng pdfkit thay thế. Chúng tôi đã cung cấp hướng dẫn từng bước để chuyển đổi trang web sang PDF bằng pdfkit. Đây là cách chuyển đổi trang web sang PDF


4. Chuyển đổi DocX sang PDF

Nếu bạn muốn chuyển đổi tệp DocX sang PDF thì nên sử dụng docx2pdf được xây dựng riêng cho mục đích này

Chúng tôi đã cung cấp hướng dẫn chi tiết về cách chuyển đổi tệp DocX sang tài liệu PDF. Đây là cách chuyển đổi tệp DocX sang PDF

Như bạn có thể thấy, python là một ngôn ngữ rất linh hoạt cho phép bạn chuyển đổi các loại dữ liệu đầu vào khác nhau sang tài liệu PDF. Xuất sang PDF là một chức năng hữu ích và được tìm kiếm trong hầu hết các trang web và ứng dụng. Bạn có thể sử dụng bất kỳ phương pháp nào ở trên để tạo PDF từ dữ liệu của mình và chia sẻ nó với người dùng của mình

Thật hữu ích khi biết cách tạo và sửa đổi các tệp PDF trong Python. PDF, hay Định dạng Tài liệu Di động, là một trong những định dạng phổ biến nhất để chia sẻ tài liệu qua Internet. Các tệp PDF có thể chứa văn bản, hình ảnh, bảng, biểu mẫu và đa phương tiện như video và hoạt ảnh, tất cả trong một tệp

Sự phong phú của các loại nội dung này có thể khiến việc làm việc với các tệp PDF trở nên khó khăn. Có rất nhiều loại dữ liệu khác nhau cần giải mã khi mở file PDF. May mắn thay, hệ sinh thái Python có một số gói tuyệt vời để đọc, thao tác và tạo tệp PDF

Trong hướng dẫn này, bạn sẽ học cách

  • Đọc văn bản từ PDF
  • Tách một tệp PDF thành nhiều tệp
  • Nối và hợp nhất các tệp PDF
  • Xoay và cắt trang trong tệp PDF
  • Mã hóa và giải mã các tệp PDF bằng mật khẩu
  • Tạo một tệp PDF từ đầu

Ghi chú. Hướng dẫn này được điều chỉnh từ chương “Tạo và sửa đổi tệp PDF” trong Python Basics. Giới thiệu thực tế về Python 3

Cuốn sách sử dụng trình soạn thảo IDLE tích hợp sẵn của Python để tạo và chỉnh sửa các tệp Python cũng như tương tác với trình bao Python, do đó, thỉnh thoảng bạn sẽ thấy các tham chiếu đến IDLE trong suốt hướng dẫn này. Tuy nhiên, bạn sẽ không gặp vấn đề gì khi chạy mã ví dụ từ trình chỉnh sửa và môi trường bạn chọn

Đồng thời, bạn sẽ có một số cơ hội để hiểu sâu hơn bằng cách làm theo các ví dụ. Bạn có thể tải xuống các tài liệu được sử dụng trong các ví dụ bằng cách nhấp vào liên kết bên dưới

Tải tài liệu mẫu. Nhấp vào đây để lấy tài liệu bạn sẽ sử dụng để tìm hiểu về cách tạo và sửa đổi tệp PDF trong hướng dẫn này

Trích xuất văn bản từ PDF

Trong phần này, bạn sẽ học cách đọc tệp PDF và trích xuất văn bản bằng gói

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26. Tuy nhiên, trước khi bạn có thể làm điều đó, bạn cần cài đặt nó với
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
27

$ python3 -m pip install PyPDF2

Xác minh cài đặt bằng cách chạy lệnh sau trong thiết bị đầu cuối của bạn

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:

Đặc biệt chú ý đến thông tin phiên bản. Tại thời điểm viết bài này, phiên bản mới nhất của

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26 là
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
29. Nếu bạn đang mở IDLE, thì bạn cần phải khởi động lại nó trước khi có thể sử dụng gói
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26

Loại bỏ các quảng cáo

Mở tệp PDF

Hãy bắt đầu bằng cách mở một tệp PDF và đọc một số thông tin về nó. Bạn sẽ sử dụng tệp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
31 nằm trong thư mục
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
32 trong kho lưu trữ đồng hành

Mở cửa sổ tương tác của IDLE và nhập lớp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 từ gói
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
4

Để tạo một phiên bản mới của lớp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33, bạn sẽ cần đường dẫn đến tệp PDF mà bạn muốn mở. Hãy làm điều đó ngay bây giờ bằng cách sử dụng mô-đun
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
36

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
7

Biến

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
37 hiện chứa đường dẫn đến phiên bản PDF của Jane Austen’s Pride and Prejudice

Ghi chú. Bạn có thể cần thay đổi

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
37 để nó tương ứng với vị trí của thư mục
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
39 trên máy tính của bạn

Bây giờ hãy tạo phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
2

Bạn chuyển đổi

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
37 thành chuỗi vì
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 không biết cách đọc từ đối tượng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
43

Nhớ lại từ chương 12, “Đầu vào và đầu ra tệp”, rằng tất cả các tệp đang mở phải được đóng trước khi chương trình kết thúc. Đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 làm tất cả những điều này cho bạn, vì vậy bạn không cần phải lo lắng về việc mở hoặc đóng tệp PDF

Bây giờ bạn đã tạo một phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33, bạn có thể sử dụng nó để thu thập thông tin về PDF. Ví dụ:
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
46 trả về số trang có trong tệp PDF

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
9

Lưu ý rằng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
46 được viết bằng mixCase, không phải Lower_case_with_underscores như khuyến nghị trong PEP 8. Hãy nhớ rằng, PEP 8 là một bộ hướng dẫn, không phải quy tắc. Đối với Python, mixCase hoàn toàn có thể chấp nhận được

Ghi chú.

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26 được điều chỉnh từ gói
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
49.
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
49 được viết vào năm 2005, chỉ bốn năm sau khi PEP 8 được xuất bản

Vào thời điểm đó, nhiều lập trình viên Python đang chuyển từ các ngôn ngữ mà mixCase phổ biến hơn

Bạn cũng có thể truy cập một số thông tin tài liệu bằng thuộc tính

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
51

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
5

Đối tượng được trả về bởi

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
51 trông giống như một cuốn từ điển, nhưng nó không thực sự giống như vậy. Bạn có thể truy cập từng mục trong
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
51 dưới dạng thuộc tính

Ví dụ: để lấy tiêu đề, hãy sử dụng thuộc tính

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
54

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
9

Đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
51 chứa siêu dữ liệu PDF, được đặt khi tạo PDF

Lớp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 cung cấp tất cả các phương thức và thuộc tính cần thiết mà bạn cần để truy cập dữ liệu trong tệp PDF. Hãy khám phá những gì bạn có thể làm với tệp PDF và cách bạn có thể làm điều đó

Loại bỏ các quảng cáo

Trích xuất văn bản từ một trang

Các trang PDF được biểu diễn bằng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26 với lớp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58. Bạn sử dụng phiên bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 để tương tác với các trang trong tệp PDF. Bạn không cần trực tiếp tạo phiên bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 của riêng mình. Thay vào đó, bạn có thể truy cập chúng thông qua phương thức
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
62 của đối tượng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33

Có hai bước để trích xuất văn bản từ một trang PDF

  1. Nhận một
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    58 với
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    64
  2. Trích xuất văn bản dưới dạng chuỗi bằng phương thức
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    66 của đối tượng
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    58

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
31 có
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
68 trang. Mỗi trang có một chỉ mục giữa
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
69 và
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
70. Bạn có thể lấy
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 đại diện cho một trang cụ thể bằng cách chuyển chỉ mục của trang tới
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
64

>>>

$ python3 -m pip install PyPDF2
8

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
62 trả về một
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
1

Bạn có thể trích xuất văn bản của trang bằng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
75

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
0

Lưu ý rằng đầu ra hiển thị ở đây đã được định dạng để phù hợp hơn trên trang này. Đầu ra bạn thấy trên máy tính của mình có thể được định dạng khác

Mỗi đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 có một thuộc tính
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
77 mà bạn có thể sử dụng để lặp qua tất cả các trang trong PDF theo thứ tự

Ví dụ: vòng lặp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
78 sau đây in văn bản từ mọi trang trong PDF Kiêu hãnh và định kiến

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
1

Hãy kết hợp mọi thứ bạn đã học và viết một chương trình trích xuất tất cả văn bản từ tệp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
31 và lưu nó vào tệp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
80

Để tất cả chúng cùng nhau

Mở một cửa sổ soạn thảo mới trong IDLE và nhập đoạn mã sau

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
2

Hãy phá vỡ điều đó

  1. Trước tiên, bạn chỉ định một phiên bản

    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    33 mới cho biến
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    82. Bạn cũng tạo một đối tượng
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    83 mới trỏ đến tệp
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    84 trong thư mục chính của bạn và gán nó cho biến
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    85

  2. Tiếp theo, bạn mở

    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    85 ở chế độ ghi và gán đối tượng tệp được trả về bởi
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    87 cho biến
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    88. Câu lệnh
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    89 mà bạn đã học trong chương 12, “Đầu vào và đầu ra tệp”, đảm bảo rằng tệp được đóng khi khối
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    89 thoát ra

  3. Sau đó, bên trong khối

    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    89, bạn viết tiêu đề PDF và số trang vào tệp văn bản bằng cách sử dụng
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    92

  4. Cuối cùng, bạn sử dụng vòng lặp

    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    78 để lặp lại tất cả các trang trong PDF. Ở mỗi bước trong vòng lặp,
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    58 tiếp theo được gán cho biến
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    95. Văn bản từ mỗi trang được trích xuất bằng
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    96 và được viết cho
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    88

Khi bạn lưu và chạy chương trình, nó sẽ tạo một tệp mới trong thư mục chính của bạn có tên là

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
84 chứa toàn bộ nội dung của tài liệu
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
31. Mở nó lên và kiểm tra xem nó ra

Loại bỏ các quảng cáo

Kiểm tra việc hiểu của bạn

Mở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn

Tập thể dục. In văn bản từ PDFHiển thị/Ẩn

Trong thư mục

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
32 trong kho lưu trữ đồng hành cho bài viết này, có một tệp tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
401. Tạo một phiên bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 để đọc PDF và sử dụng nó để in văn bản từ trang đầu tiên

Bạn có thể mở rộng khối bên dưới để xem giải pháp

Giải pháp. In văn bản từ PDFHiển thị/Ẩn

Thiết lập đường dẫn đến tệp PDF

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
3

Bây giờ bạn có thể tạo phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
4

Hãy nhớ rằng các đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 chỉ có thể được khởi tạo bằng chuỗi đường dẫn, không phải đối tượng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
83

Bây giờ, hãy sử dụng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
62 để lấy trang đầu tiên

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
5

Hãy nhớ rằng, các trang được lập chỉ mục bắt đầu bằng 0

Sau đó sử dụng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
66 để trích xuất văn bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
6

Cuối cùng, in văn bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
7

Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo

Trích xuất các trang từ PDF

Trong phần trước, bạn đã học cách trích xuất toàn bộ văn bản từ tệp PDF và lưu vào tệp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
80. Bây giờ, bạn sẽ tìm hiểu cách trích xuất một trang hoặc nhiều trang từ một tệp PDF hiện có và lưu chúng vào một tệp PDF mới

Bạn có thể sử dụng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 để tạo tệp PDF mới. Hãy cùng khám phá lớp học này và tìm hiểu các bước cần thiết để tạo PDF bằng cách sử dụng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26

Sử dụng lớp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409

Lớp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 tạo tệp PDF mới. Trong cửa sổ tương tác của IDLE, nhập lớp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 và tạo một phiên bản mới có tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
8

Các đối tượng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 giống như các tệp PDF trống. Bạn cần thêm một số trang vào chúng trước khi có thể lưu chúng vào tệp

Hãy tiếp tục và thêm một trang trống vào

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
9

Các tham số

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
417 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
418 là bắt buộc và xác định kích thước của trang theo đơn vị gọi là điểm. Một điểm bằng 1/72 inch, vì vậy đoạn mã trên thêm một trang trống một inch vuông vào
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
420 trả về một phiên bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 mới đại diện cho trang mà bạn đã thêm vào
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
40

Trong ví dụ này, bạn đã chỉ định thể hiện

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 được trả về bởi
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
420 cho biến
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
95, nhưng trong thực tế, bạn thường không cần phải làm điều này. Nghĩa là, bạn thường gọi
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
420 mà không gán giá trị trả về cho bất kỳ thứ gì

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
41

Để ghi nội dung của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 vào tệp PDF, hãy chuyển một đối tượng tệp ở chế độ ghi nhị phân sang
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
428

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
42

Thao tác này tạo một tệp mới trong thư mục làm việc hiện tại của bạn có tên là

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
429. Nếu bạn mở tệp bằng trình đọc PDF, chẳng hạn như Adobe Acrobat, thì bạn sẽ thấy tài liệu có một trang trống một inch vuông

Chi tiết kỹ thuật. Lưu ý rằng bạn lưu tệp PDF bằng cách chuyển đối tượng tệp sang phương thức

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
431 của đối tượng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409, chứ không phải phương thức
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
431 của đối tượng tệp

Đặc biệt, đoạn mã sau sẽ không hoạt động

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
43

Cách tiếp cận này có vẻ lạc hậu đối với nhiều lập trình viên mới, vì vậy hãy chắc chắn rằng bạn tránh được sai lầm này

Các đối tượng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 có thể ghi vào các tệp PDF mới, nhưng chúng không thể tạo nội dung mới từ đầu ngoài các trang trống

Đây có vẻ là một vấn đề lớn, nhưng trong nhiều trường hợp, bạn không cần phải tạo nội dung mới. Thông thường, bạn sẽ làm việc với các trang được trích xuất từ ​​tệp PDF mà bạn đã mở bằng phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33

Ghi chú. Bạn sẽ tìm hiểu cách tạo tệp PDF từ đầu bên dưới, trong phần “Tạo tệp PDF từ đầu. ”

Trong ví dụ bạn đã thấy ở trên, có ba bước để tạo một tệp PDF mới bằng cách sử dụng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26

  1. Tạo một phiên bản
    #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    409
  2. Thêm một hoặc nhiều trang vào phiên bản
    #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    409
  3. Ghi vào tệp bằng cách sử dụng
    #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    438

Bạn sẽ thấy mẫu này lặp đi lặp lại khi tìm hiểu nhiều cách khác nhau để thêm trang vào phiên bản

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409

Loại bỏ các quảng cáo

Trích xuất một trang từ PDF

Hãy cùng xem lại bản PDF Kiêu hãnh và định kiến ​​mà bạn đã làm việc trong phần trước. Bạn sẽ mở tệp PDF, trích xuất trang đầu tiên và tạo một tệp PDF mới chỉ chứa một trang được trích xuất duy nhất

Mở cửa sổ tương tác của IDLE và nhập

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 từ
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26 cũng như lớp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
83 từ mô-đun
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
36

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
44

Bây giờ hãy mở tệp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
31 bằng phiên bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
45

Chuyển chỉ số

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
69 đến
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
62 để nhận được một
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 đại diện cho trang đầu tiên của PDF

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
46

Bây giờ hãy tạo một phiên bản

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 mới và thêm
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
451 vào đó với
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
452

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
47

Phương thức

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
452 thêm một trang vào tập hợp các trang trong đối tượng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414, giống như
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
420. Sự khác biệt là nó yêu cầu một
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 hiện có

Bây giờ hãy ghi nội dung của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 vào một tệp mới

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
48

Bây giờ bạn có một tệp PDF mới được lưu trong thư mục làm việc hiện tại của bạn có tên là

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
458, chứa trang bìa của tệp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
31. khá gọn gàng

Trích xuất nhiều trang từ một tệp PDF

Hãy trích xuất chương đầu tiên từ

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
31 và lưu nó vào một tệp PDF mới

Nếu bạn mở

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
31 bằng trình xem PDF, thì bạn có thể thấy chương đầu tiên nằm ở trang thứ hai, thứ ba và thứ tư của PDF. Vì các trang được lập chỉ mục bắt đầu bằng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
69, nên bạn sẽ cần trích xuất các trang ở chỉ mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
463,
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
464 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
465

Bạn có thể thiết lập mọi thứ bằng cách nhập các lớp bạn cần và mở tệp PDF

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
49

Mục tiêu của bạn là trích xuất các trang tại các chỉ số

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
463,
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
464 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
465, thêm các trang này vào một phiên bản
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 mới, sau đó ghi chúng vào một tệp PDF mới

Một cách để thực hiện điều này là lặp qua phạm vi số bắt đầu từ ________ 1463 và kết thúc ở ________ 1465, trích xuất trang ở mỗi bước của vòng lặp và thêm nó vào phiên bản

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
70

Vòng lặp lặp qua các số

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
463,
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
464 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
465 vì
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
476 không bao gồm điểm cuối bên phải. Ở mỗi bước trong vòng lặp, trang tại chỉ mục hiện tại được trích xuất bằng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
62 và được thêm vào
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 bằng cách sử dụng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
452

Bây giờ

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 có ba trang, bạn có thể kiểm tra với
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
46

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
71

Cuối cùng, bạn có thể ghi các trang được trích xuất vào một tệp PDF mới

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
72

Bây giờ bạn có thể mở tệp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
482 trong thư mục làm việc hiện tại của mình để chỉ đọc chương đầu tiên của Kiêu hãnh và định kiến

Một cách khác để trích xuất nhiều trang từ PDF là tận dụng lợi thế của việc

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
483 hỗ trợ ký hiệu lát cắt. Hãy làm lại ví dụ trước bằng cách sử dụng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
77 thay vì lặp qua một đối tượng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
485

Bắt đầu bằng cách khởi tạo một đối tượng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 mới

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
73

Bây giờ lặp qua một lát của

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
77 từ các chỉ số bắt đầu từ
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
463 và kết thúc tại
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
489

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
74

Hãy nhớ rằng các giá trị trong một lát nằm trong phạm vi từ mục ở chỉ mục đầu tiên trong lát cho đến, nhưng không bao gồm, mục ở chỉ mục thứ hai trong lát. Vì vậy,

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
490 trả về một iterable chứa các trang có chỉ số
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
463,
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
464 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
465

Cuối cùng, ghi nội dung của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 vào tệp đầu ra

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
75

Bây giờ hãy mở tệp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
495 trong thư mục làm việc hiện tại của bạn và so sánh nó với tệp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
482 mà bạn đã tạo bằng cách lặp qua đối tượng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
485. Chúng chứa các trang giống nhau

Đôi khi bạn cần trích xuất mọi trang từ PDF. Bạn có thể sử dụng các phương pháp được minh họa ở trên để thực hiện việc này, nhưng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26 cung cấp một lối tắt. Phiên bản
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 có phương thức
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
700 mà bạn có thể sử dụng để nối các trang từ phiên bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33

Để sử dụng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
700, hãy truyền một phiên bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 cho tham số
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
704 của phương thức. Ví dụ: đoạn mã sau sao chép mọi trang từ PDF Kiêu hãnh và Định kiến ​​sang phiên bản
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
76

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 hiện chứa mọi trang trong
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
82

Loại bỏ các quảng cáo

Kiểm tra việc hiểu của bạn

Mở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn

Tập thể dục. Trích xuất trang cuối cùng của PDFHiển thị/Ẩn

Trích xuất trang cuối cùng từ tệp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
31 và lưu nó vào một tệp mới có tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
709 trong thư mục chính của bạn

Bạn có thể mở rộng khối bên dưới để xem giải pháp

Giải pháp. Trích xuất trang cuối cùng của PDFHiển thị/Ẩn

Thiết lập đường dẫn đến tệp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
31

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
77

Bây giờ bạn có thể tạo phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
4

Hãy nhớ rằng các đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 chỉ có thể được khởi tạo bằng chuỗi đường dẫn, không phải đối tượng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
83

Sử dụng thuộc tính

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
77 để có thể lặp lại trên tất cả các trang trong PDF. Trang cuối cùng có thể được truy cập với chỉ mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
715

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
79

Bây giờ bạn có thể tạo một phiên bản

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 và thêm trang cuối cùng vào đó

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
20

Cuối cùng, ghi nội dung của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 vào tệp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
709 trong thư mục chính của bạn

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
21

Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo

Nối và hợp nhất các tệp PDF

Hai tác vụ phổ biến khi làm việc với tệp PDF là nối và hợp nhất nhiều tệp PDF thành một tệp duy nhất

Khi bạn nối hai hoặc nhiều tệp PDF, bạn nối các tệp này với nhau thành một tài liệu duy nhất. Ví dụ: một công ty có thể kết hợp nhiều báo cáo hàng ngày thành một báo cáo hàng tháng vào cuối tháng

Hợp nhất hai tệp PDF cũng kết hợp các tệp PDF thành một tệp duy nhất. Nhưng thay vì nối tệp PDF thứ hai vào cuối tệp PDF đầu tiên, việc hợp nhất cho phép bạn chèn tệp đó vào sau một trang cụ thể trong tệp PDF đầu tiên. Sau đó, nó sẽ đẩy tất cả các trang của tệp PDF đầu tiên sau điểm chèn vào cuối tệp PDF thứ hai

Trong phần này, bạn sẽ tìm hiểu cách ghép nối và hợp nhất các tệp PDF bằng cách sử dụng gói

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720 của gói
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26

Sử dụng lớp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720

Lớp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720 rất giống với lớp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 mà bạn đã học ở phần trước. Bạn có thể sử dụng cả hai lớp để ghi tệp PDF. Trong cả hai trường hợp, bạn thêm các trang vào các thể hiện của lớp và sau đó ghi chúng vào một tệp

Sự khác biệt chính giữa hai loại này là

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 chỉ có thể nối thêm hoặc nối các trang vào cuối danh sách các trang đã có trong trình soạn thảo, trong khi đó
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720 có thể chèn hoặc nối các trang ở bất kỳ vị trí nào

Hãy tiếp tục và tạo phiên bản

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720 đầu tiên của bạn. Trong cửa sổ tương tác của IDLE, hãy nhập mã sau để nhập lớp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720 và tạo một phiên bản mới

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
22

Các đối tượng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720 trống khi chúng được khởi tạo lần đầu tiên. Bạn sẽ cần thêm một số trang vào đối tượng của mình trước khi có thể làm bất cứ điều gì với nó

Có một số cách để thêm trang vào đối tượng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
729 và cách bạn sử dụng tùy thuộc vào những gì bạn cần thực hiện

  • #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    730 ghép nối mọi trang trong tài liệu PDF hiện có với phần cuối của các trang hiện tại trong tài liệu PDF hiện tại
    #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    720
  • #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    732 chèn tất cả các trang trong tài liệu PDF hiện có sau một trang cụ thể trong
    #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    720

Bạn sẽ xem xét cả hai phương pháp trong phần này, bắt đầu với

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
730

Loại bỏ các quảng cáo

Nối các tệp PDF với
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
730

Thư mục

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
32 có một thư mục con có tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
737 chứa ba báo cáo chi phí cho một nhân viên tên là Peter Python

Peter cần nối ba tệp PDF này và gửi chúng cho chủ lao động của mình dưới dạng một tệp PDF duy nhất để anh ấy có thể được hoàn trả một số chi phí liên quan đến công việc

Bạn có thể bắt đầu bằng cách sử dụng mô-đun

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
36 để nhận danh sách các đối tượng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
83 cho từng báo cáo trong số ba báo cáo chi phí trong thư mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
740

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
23

Sau khi nhập lớp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
83, bạn cần xây dựng đường dẫn đến thư mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
740. Lưu ý rằng bạn có thể cần thay đổi mã ở trên để có được đường dẫn chính xác trên máy tính của mình

Khi bạn có đường dẫn đến thư mục

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
740 được gán cho biến
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
744, bạn có thể sử dụng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
745 để có thể lặp lại đường dẫn đến tệp PDF trong thư mục

Hãy xem những gì trong thư mục

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
24

Tên của ba tệp được liệt kê, nhưng chúng không theo thứ tự. Hơn nữa, thứ tự của các tệp bạn thấy trong đầu ra trên máy tính của bạn có thể không khớp với đầu ra được hiển thị ở đây

Nói chung, thứ tự của các đường dẫn được trả về bởi

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
745 không được đảm bảo, vì vậy bạn cần phải tự sắp xếp chúng. Bạn có thể làm điều này bằng cách tạo một danh sách chứa ba đường dẫn tệp và sau đó gọi
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
747 trong danh sách đó

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
25

Hãy nhớ rằng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
747 sắp xếp danh sách tại chỗ, vì vậy bạn không cần gán giá trị trả về cho một biến. Danh sách
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
737 sẽ được sắp xếp theo thứ tự abc theo tên tệp sau khi
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
750 được gọi

Để xác nhận rằng việc sắp xếp đã hoạt động, hãy lặp lại

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
737 và in ra tên tệp

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
26

Trông tôt đây

Bây giờ bạn có thể nối ba tệp PDF. Để làm điều đó, bạn sẽ sử dụng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
752, yêu cầu một đối số chuỗi biểu thị đường dẫn đến tệp PDF. Khi bạn gọi
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
730, tất cả các trang trong tệp PDF được thêm vào tập hợp các trang trong đối tượng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720

Hãy xem điều này trong hành động. Đầu tiên, nhập lớp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720 và tạo một thể hiện mới

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
22

Bây giờ lặp qua các đường dẫn trong danh sách

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
737 đã sắp xếp và nối chúng vào
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
729

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
28

Lưu ý rằng mỗi đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
83 trong
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
740 được chuyển đổi thành một chuỗi có
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
760 trước khi được chuyển đến
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
761

Với tất cả các tệp PDF trong thư mục

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
740 được nối trong đối tượng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
729, điều cuối cùng bạn cần làm là ghi mọi thứ vào tệp PDF đầu ra. Phiên bản
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720 có phương thức
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
431 hoạt động giống như phiên bản
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
438

Mở một tệp mới ở chế độ ghi nhị phân, sau đó chuyển đối tượng tệp sang phương thức

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
767

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
29

Bây giờ bạn có một tệp PDF trong thư mục làm việc hiện tại của mình có tên là

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
768. Mở nó ra bằng một trình đọc PDF và bạn sẽ tìm thấy cả ba báo cáo chi phí trong cùng một tệp PDF

Loại bỏ các quảng cáo

Hợp nhất các tệp PDF với
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
732

Để hợp nhất hai hoặc nhiều tệp PDF, hãy sử dụng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
770. Phương pháp này tương tự như
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
730, ngoại trừ việc bạn phải chỉ định vị trí trong tệp PDF đầu ra để chèn tất cả nội dung từ tệp PDF mà bạn đang hợp nhất

Hãy xem một ví dụ. Goggle, Inc. chuẩn bị báo cáo quý nhưng quên kèm theo mục lục. Peter Python đã nhận thấy lỗi và nhanh chóng tạo một tệp PDF với mục lục bị thiếu. Bây giờ anh ấy cần hợp nhất tệp PDF đó vào báo cáo gốc

Có thể tìm thấy cả PDF báo cáo và mục lục PDF trong thư mục con

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
772 của thư mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
773. Báo cáo nằm trong một tệp có tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
774 và mục lục nằm trong một tệp có tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
775

Trong cửa sổ tương tác của IDLE, nhập lớp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720 và tạo các đối tượng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
83 cho các tệp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
774 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
775

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
90

Điều đầu tiên bạn sẽ làm là thêm báo cáo PDF vào phiên bản

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720 mới bằng cách sử dụng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
730

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
91

Giờ đây,

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
729 có một số trang trong đó, bạn có thể hợp nhất mục lục PDF vào đó ở đúng vị trí. Nếu bạn mở tệp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
774 bằng trình đọc PDF, thì bạn sẽ thấy trang đầu tiên của báo cáo là trang tiêu đề. Trang thứ hai là phần giới thiệu và các trang còn lại chứa các phần báo cáo khác nhau

Bạn muốn chèn mục lục sau trang tiêu đề và ngay trước phần giới thiệu. Vì các chỉ mục trang PDF bắt đầu bằng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
69 trong
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26, bạn cần chèn mục lục sau trang có chỉ mục
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
69 và trước trang có chỉ mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
463

Để làm điều đó, hãy gọi

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
788 với hai đối số

  1. Số nguyên
    #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    463, cho biết chỉ mục của trang mà mục lục sẽ được chèn vào
  2. Một chuỗi chứa đường dẫn của tệp PDF cho mục lục

Đây là những gì trông giống như

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
92

Mỗi trang trong mục lục PDF được chèn vào trước trang ở chỉ mục

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
463. Vì mục lục PDF chỉ có một trang nên nó được chèn vào chỉ mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
463. Trang hiện đang ở chỉ mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
463 sau đó được chuyển sang chỉ mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
464. Trang hiện đang ở chỉ mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
464 được chuyển sang chỉ mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
465, v.v.

Bây giờ hãy viết tệp PDF đã hợp nhất thành một tệp đầu ra

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
93

Bây giờ bạn có tệp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
796 trong thư mục làm việc hiện tại của mình. Mở nó lên bằng trình đọc PDF và kiểm tra xem mục lục đã được chèn vào đúng vị trí chưa

Nối và hợp nhất các tệp PDF là các hoạt động phổ biến. Mặc dù phải thừa nhận rằng các ví dụ trong phần này hơi giả tạo, nhưng bạn có thể hình dung một chương trình sẽ hữu ích như thế nào khi hợp nhất hàng nghìn tệp PDF hoặc để tự động hóa các tác vụ thông thường mà nếu không thì con người sẽ mất rất nhiều thời gian để hoàn thành

Kiểm tra việc hiểu của bạn

Mở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn

Tập thể dục. Nối hai tệp PDFHiển thị/Ẩn

Trong thư mục

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
32 trong kho lưu trữ đồng hành cho bài viết này, có hai tệp có tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
798 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
799

Sử dụng phiên bản

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
200, nối hai tệp bằng cách sử dụng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
730. Lưu các tệp PDF được nối vào một tệp mới có tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
202 nếu thư mục chính trên máy tính của bạn

Bạn có thể mở rộng khối bên dưới để xem giải pháp

Giải pháp. Nối hai tệp PDFHiển thị/Ẩn

Thiết lập đường dẫn đến tệp PDF

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
94

Bây giờ bạn có thể tạo phiên bản

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
720

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
95

Bây giờ, lặp qua các đường dẫn trong mỗi tệp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
204 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
730 đến
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
729

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
96

Cuối cùng, viết nội dung của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
729 vào một tệp có tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
202 trong thư mục chính của bạn

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
97

Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo

Loại bỏ các quảng cáo

Xoay và cắt xén trang PDF

Cho đến nay, bạn đã học cách trích xuất văn bản và trang từ PDF cũng như cách nối và hợp nhất hai hoặc nhiều tệp PDF. Đây là tất cả các thao tác phổ biến với PDF, nhưng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26 có nhiều tính năng hữu ích khác

Ghi chú. Hướng dẫn này được điều chỉnh từ chương “Tạo và sửa đổi tệp PDF” trong Python Basics. Giới thiệu thực tế về Python 3. Nếu bạn thích những gì bạn đang đọc, thì hãy nhớ xem phần còn lại của cuốn sách

Trong phần này, bạn sẽ học cách xoay và cắt các trang trong tệp PDF

Xoay trang

Bạn sẽ bắt đầu bằng cách học cách xoay trang. Đối với ví dụ này, bạn sẽ sử dụng tệp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
210 trong thư mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
773. Tệp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
210 chứa một phiên bản đáng yêu của tác phẩm Vịt con xấu xí của Hans Christian Andersen, ngoại trừ việc mỗi trang đánh số lẻ đều được xoay ngược chiều kim đồng hồ 90 độ

Hãy khắc phục điều đó. Trong cửa sổ tương tác IDLE mới, hãy bắt đầu bằng cách nhập các lớp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 từ
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26, cũng như lớp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
83 từ mô-đun
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
36

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
44

Bây giờ hãy tạo một đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
83 cho tệp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
210

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
99

Cuối cùng, tạo các phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 mới

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
50

Mục tiêu của bạn là sử dụng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 để tạo một tệp PDF mới trong đó tất cả các trang đều có hướng chính xác. Các trang đánh số chẵn trong PDF đã được định hướng đúng, nhưng các trang đánh số lẻ được xoay chín mươi độ ngược chiều kim đồng hồ

Để khắc phục sự cố, bạn sẽ sử dụng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
223. Phương thức này nhận một đối số số nguyên, theo độ và xoay trang theo chiều kim đồng hồ theo độ đó. Ví dụ:
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
224 xoay trang PDF theo chiều kim đồng hồ 90 độ

Ghi chú. Ngoài

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
225, lớp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 còn có
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
227 để xoay trang ngược chiều kim đồng hồ

Có một số cách bạn có thể thực hiện để xoay trang trong PDF. Chúng ta sẽ thảo luận về hai cách khác nhau để thực hiện. Cả hai đều dựa trên

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
225, nhưng họ thực hiện các cách tiếp cận khác nhau để xác định trang nào sẽ được xoay vòng

Kỹ thuật đầu tiên là lặp qua các chỉ mục của các trang trong PDF và kiểm tra xem mỗi chỉ mục có tương ứng với một trang cần được xoay không. Nếu vậy, bạn sẽ gọi

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
225 để xoay trang và sau đó thêm trang vào
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414

Đây là những gì trông giống như

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
51

Lưu ý rằng trang sẽ được xoay nếu chỉ mục là số chẵn. Điều đó có vẻ lạ vì các trang được đánh số lẻ trong PDF là những trang được xoay không chính xác. Tuy nhiên, số trang trong PDF bắt đầu bằng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
463, trong khi chỉ số trang bắt đầu bằng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
69. Điều đó có nghĩa là các trang PDF được đánh số lẻ có các chỉ số chẵn

Nếu điều đó khiến bạn quay cuồng, đừng lo lắng. Ngay cả sau nhiều năm xử lý những thứ như thế này, các lập trình viên chuyên nghiệp vẫn bị vấp ngã bởi những thứ này

Ghi chú. Khi bạn thực hiện vòng lặp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
78 ở trên, bạn sẽ thấy một loạt kết quả trong cửa sổ tương tác của IDLE. Đó là bởi vì
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
225 trả về một phiên bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58

Bạn có thể bỏ qua đầu ra này ngay bây giờ. Khi bạn thực thi các chương trình từ cửa sổ soạn thảo của IDLE, đầu ra này sẽ không hiển thị

Bây giờ bạn đã xoay tất cả các trang trong PDF, bạn có thể ghi nội dung của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 vào một tệp mới và kiểm tra xem mọi thứ đã hoạt động chưa

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
52

Bây giờ bạn sẽ có một tệp trong thư mục làm việc hiện tại của mình có tên là

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
237, với tất cả các trang từ tệp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
210 đều được xoay chính xác

Vấn đề với phương pháp bạn vừa sử dụng để xoay trang trong tệp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
210 là nó phụ thuộc vào việc biết trước trang nào cần được xoay. Trong một kịch bản trong thế giới thực, việc xem qua toàn bộ tệp PDF để ghi lại những trang nào cần xoay là không thực tế

Trên thực tế, bạn có thể xác định trang nào cần được xoay mà không cần biết trước. Vâng, đôi khi bạn có thể

Hãy xem cách thực hiện, bắt đầu với phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 mới

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
53

Bạn cần làm điều này vì bạn đã thay đổi các trang trong phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 cũ bằng cách xoay chúng. Vì vậy, bằng cách tạo một phiên bản mới, bạn đang bắt đầu làm mới

Phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 duy trì một từ điển các giá trị chứa thông tin về trang

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
54

Rất tiếc. Trộn lẫn với tất cả những thứ trông vô nghĩa đó là một khóa có tên là

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
243, bạn có thể thấy khóa này ở dòng đầu ra thứ tư ở trên. Giá trị của khóa này là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
244

Bạn có thể truy cập khóa

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
243 trên một
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 bằng cách sử dụng ký hiệu chỉ số dưới, giống như bạn có thể làm trên một đối tượng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
247 của Python

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
55

Nếu bạn nhìn vào khóa

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
243 cho trang thứ hai trong
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
82, bạn sẽ thấy rằng nó có giá trị là
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
69

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
56

Tất cả điều này có nghĩa là trang ở chỉ mục

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
69 có giá trị xoay là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
244 độ. Nói cách khác, nó đã được xoay chín mươi độ ngược chiều kim đồng hồ. Trang tại chỉ mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
463 có giá trị xoay vòng là
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
69, vì vậy nó hoàn toàn không được xoay

Nếu bạn xoay trang đầu tiên bằng cách sử dụng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
225, thì giá trị của
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
243 sẽ thay đổi từ
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
244 thành
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
69

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
57

Bây giờ bạn đã biết cách kiểm tra khóa

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
243, bạn có thể sử dụng nó để xoay các trang trong tệp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
210

Điều đầu tiên bạn cần làm là khởi tạo lại các đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
82 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 để bạn có một khởi đầu mới

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
50

Bây giờ hãy viết một vòng lặp lặp qua các trang trong

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
263 iterable, kiểm tra giá trị của
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
243 và xoay trang nếu giá trị đó là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
244

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
59

Vòng lặp này không chỉ ngắn hơn một chút so với vòng lặp trong giải pháp đầu tiên mà còn không dựa vào bất kỳ kiến ​​thức nào trước đó về trang nào cần được xoay. Bạn có thể sử dụng một vòng lặp như thế này để xoay các trang trong bất kỳ tệp PDF nào mà không cần phải mở và xem nó

Để hoàn thành giải pháp, hãy ghi nội dung của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 vào một tệp mới

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
90

Bây giờ bạn có thể mở tệp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
267 trong thư mục làm việc hiện tại của mình và so sánh nó với tệp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
237 mà bạn đã tạo trước đó. Họ nên trông giống hệt nhau

Ghi chú. Một lời cảnh báo về khóa

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
243. nó không được đảm bảo để tồn tại trên một trang

Nếu khóa

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
243 không tồn tại, thì điều đó thường có nghĩa là trang chưa được xoay. Tuy nhiên, đó không phải lúc nào cũng là một giả định an toàn

Nếu một

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 không có khóa
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
243, thì một
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
273 sẽ xuất hiện khi bạn cố gắng truy cập nó. Bạn có thể bắt ngoại lệ này bằng khối
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
274

Giá trị của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
243 có thể không phải lúc nào cũng như bạn mong đợi. Ví dụ: nếu bạn quét tài liệu giấy với trang được xoay 90 độ ngược chiều kim đồng hồ thì nội dung của tệp PDF sẽ xuất hiện xoay. Tuy nhiên, khóa
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
243 có thể có giá trị
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
69

Đây là một trong nhiều điều kỳ quặc có thể khiến việc làm việc với các tệp PDF trở nên khó chịu. Đôi khi, bạn sẽ chỉ cần mở một tệp PDF trong chương trình đọc PDF và tìm ra mọi thứ theo cách thủ công

Loại bỏ các quảng cáo

Cắt xén trang

Một thao tác phổ biến khác với các tệp PDF là cắt xén trang. Bạn có thể cần làm điều này để chia một trang thành nhiều trang hoặc chỉ trích xuất một phần nhỏ của trang, chẳng hạn như chữ ký hoặc hình

Ví dụ: thư mục

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
773 bao gồm một tệp có tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
279. Bản PDF này chứa một phần của Nàng tiên cá nhỏ của Hans Christian Andersen

Mỗi trang trong PDF này có hai cột. Hãy chia mỗi trang thành hai trang, một trang cho mỗi cột

Để bắt đầu, hãy nhập các lớp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 từ
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26 và lớp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
83 từ mô-đun
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
36

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
44

Bây giờ hãy tạo một đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
83 cho tệp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
279

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
92

Tiếp theo, tạo một đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 mới và lấy trang đầu tiên của PDF

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
93

Để cắt trang, trước tiên bạn cần biết thêm một chút về cách các trang được cấu trúc. Các trường hợp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
58 như
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
451 có thuộc tính
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
290 đại diện cho một khu vực hình chữ nhật xác định ranh giới của trang

Bạn có thể sử dụng cửa sổ tương tác của IDLE để khám phá

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
290 trước khi sử dụng nó để cắt trang

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
94

Thuộc tính

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
290 trả về một
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
293. Đối tượng này được xác định trong gói
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26 và đại diện cho một vùng hình chữ nhật trên trang

Danh sách

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
295 trong đầu ra xác định diện tích hình chữ nhật. Hai số đầu tiên là tọa độ x và y của góc dưới bên trái của hình chữ nhật. Số thứ ba và số thứ tư lần lượt là chiều rộng và chiều cao của hình chữ nhật. Đơn vị của tất cả các giá trị là điểm, bằng 1/72 inch

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
296 đại diện cho một vùng hình chữ nhật có gốc tọa độ ở góc dưới bên trái, chiều rộng là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
297 điểm hoặc 11 inch và chiều cao là 612 điểm hoặc 8. 5 inch. Đó là các kích thước của một trang cỡ chữ tiêu chuẩn theo hướng ngang, được sử dụng cho tệp PDF mẫu Nàng tiên cá nhỏ. Một trang PDF có kích thước bằng chữ theo hướng dọc sẽ trả về kết quả đầu ra
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
298

Một

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
293 có bốn thuộc tính trả về tọa độ của các góc của hình chữ nhật.
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
900,
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
901,
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
902 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
903. Cũng giống như các giá trị chiều rộng và chiều cao, các tọa độ này được tính theo điểm

Bạn có thể sử dụng bốn thuộc tính này để lấy tọa độ của từng góc của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
293

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
95

Mỗi thuộc tính trả về một

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
905 chứa tọa độ của góc được chỉ định. Bạn có thể truy cập các tọa độ riêng lẻ bằng dấu ngoặc vuông giống như bất kỳ bộ Python nào khác

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
96

Bạn có thể thay đổi tọa độ của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
906 bằng cách gán một bộ dữ liệu mới cho một trong các thuộc tính của nó

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
97

Khi bạn thay đổi tọa độ

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
902, thuộc tính
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
903 sẽ tự động điều chỉnh để duy trì hình dạng hình chữ nhật

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
98

Khi bạn thay đổi tọa độ của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
293 được trả về bởi
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
290, bạn sẽ cắt trang một cách hiệu quả. Đối tượng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
451 hiện chỉ chứa thông tin có trong ranh giới của
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
293 mới

Hãy tiếp tục và ghi trang đã cắt vào một tệp PDF mới

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
99

Nếu bạn mở tệp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
913 trong thư mục làm việc hiện tại của mình, thì bạn sẽ thấy rằng phần trên cùng của trang đã bị xóa

Bạn sẽ cắt trang như thế nào để chỉ hiển thị văn bản ở phía bên trái của trang? . Bạn có thể đạt được điều này bằng cách thay đổi tọa độ

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
903 của đối tượng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
290. Hãy xem nó hoạt động như thế nào

Trước tiên, bạn cần lấy các đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 mới vì bạn vừa thay đổi trang đầu tiên trong
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
82 và thêm nó vào
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
50

Bây giờ lấy trang đầu tiên của PDF

>>>

$ python3 -m pip install PyPDF2
81

Lần này, hãy làm việc với một bản sao của trang đầu tiên để trang bạn vừa trích xuất vẫn nguyên vẹn. Bạn có thể làm điều đó bằng cách nhập mô-đun

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
920 từ thư viện chuẩn của Python và sử dụng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
921 để tạo một bản sao của trang

>>>

$ python3 -m pip install PyPDF2
82

Bây giờ bạn có thể thay đổi

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
922 mà không thay đổi thuộc tính của
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
451. Bằng cách đó, bạn có thể sử dụng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
451 sau này để trích xuất văn bản ở phía bên phải của trang

Bây giờ bạn cần làm một chút toán học. Bạn đã nhận ra rằng bạn cần di chuyển góc trên bên phải của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
290 lên chính giữa trên cùng của trang. Để làm điều đó, bạn sẽ tạo một
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
905 mới với thành phần đầu tiên bằng một nửa giá trị ban đầu và gán nó cho thuộc tính
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
903

Đầu tiên, lấy tọa độ hiện tại của góc trên bên phải của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
290

>>>

$ python3 -m pip install PyPDF2
83

Sau đó, tạo một

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
905 mới có tọa độ đầu tiên bằng một nửa giá trị của tọa độ hiện tại và tọa độ thứ hai giống với tọa độ ban đầu

>>>

$ python3 -m pip install PyPDF2
84

Cuối cùng, gán tọa độ mới cho thuộc tính

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
903

>>>

$ python3 -m pip install PyPDF2
85

Bây giờ bạn đã cắt trang gốc để chỉ chứa văn bản ở bên trái. Tiếp theo, hãy trích xuất phần bên phải của trang

Trước tiên hãy lấy một bản sao mới của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
451

>>>

$ python3 -m pip install PyPDF2
86

Di chuyển góc

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
902 thay vì góc
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
903

>>>

$ python3 -m pip install PyPDF2
87

Thao tác này đặt góc trên bên trái thành cùng tọa độ mà bạn đã di chuyển góc trên bên phải khi trích xuất phần bên trái của trang. Vì vậy,

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
934 bây giờ là một hình chữ nhật có góc trên bên trái nằm ở giữa trên cùng của trang và có góc trên bên phải nằm ở trên cùng bên phải của trang

Cuối cùng, thêm các trang

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
922 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
936 vào
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 và ghi chúng vào một tệp PDF mới

>>>

$ python3 -m pip install PyPDF2
88

Bây giờ hãy mở tệp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
938 bằng trình đọc PDF. Bạn sẽ thấy một tệp có hai trang, trang đầu tiên chứa văn bản từ phía bên trái của trang đầu tiên ban đầu và trang thứ hai chứa văn bản từ phía bên phải ban đầu

Loại bỏ các quảng cáo

Kiểm tra việc hiểu của bạn

Mở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn

Tập thể dục. Xoay các trang trong PDFHiển thị/Ẩn

Trong thư mục

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
32 trong kho lưu trữ đồng hành cho bài viết này, có một tệp tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
940

Tạo một tệp mới có tên là

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
941 trong thư mục chính của máy tính chứa tất cả các trang từ
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
940, nhưng mỗi trang được xoay ngược chiều kim đồng hồ 90 độ

Bạn có thể mở rộng khối bên dưới để xem giải pháp

Giải pháp. Xoay các trang trong PDFHiển thị/Ẩn

Thiết lập đường dẫn đến tệp PDF

$ python3 -m pip install PyPDF2
89

Bây giờ bạn có thể tạo các phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
10

Lặp lại các trang trong ________ 382, ​​xoay tất cả chúng 90 độ bằng cách sử dụng ________ 2227 và thêm chúng vào

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
11

Cuối cùng, viết nội dung của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 vào một tệp có tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
941 trong thư mục chính của máy tính của bạn

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
12

Mã hóa và giải mã PDF

Đôi khi các tệp PDF được bảo vệ bằng mật khẩu. Với gói

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26, bạn có thể làm việc với các tệp PDF được mã hóa cũng như thêm mật khẩu bảo vệ cho các tệp PDF hiện có

Ghi chú. Hướng dẫn này được điều chỉnh từ chương “Tạo và sửa đổi tệp PDF” trong Python Basics. Giới thiệu thực tế về Python 3. Nếu bạn thích những gì bạn đang đọc, thì hãy nhớ xem phần còn lại của cuốn sách

Mã hóa tệp PDF

Bạn có thể thêm mật khẩu bảo vệ vào tệp PDF bằng phương pháp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
951 của phiên bản
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
952. Nó có hai tham số chính

  1. #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # open the text file in read mode
    f = open["data.txt", "r"]
     
    # insert the texts in pdf
    for x in f:
           pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
     
      
    # save the pdf with name .pdf
    pdf.output["data.pdf"]   
    953 đặt mật khẩu người dùng. Điều này cho phép mở và đọc tệp PDF
  2. #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # open the text file in read mode
    f = open["data.txt", "r"]
     
    # insert the texts in pdf
    for x in f:
           pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
     
      
    # save the pdf with name .pdf
    pdf.output["data.pdf"]   
    954 đặt mật khẩu chủ sở hữu. Điều này cho phép mở tệp PDF mà không có bất kỳ hạn chế nào, kể cả chỉnh sửa

Hãy sử dụng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
951 để thêm mật khẩu vào tệp PDF. Đầu tiên, mở tệp
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
956 trong thư mục
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
773

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
13

Bây giờ hãy tạo một phiên bản

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409 mới và thêm các trang từ
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
82 vào đó

>>>

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
76

Tiếp theo, thêm mật khẩu

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
960 với
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
961

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
15

Khi bạn chỉ đặt

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
953, đối số
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
954 sẽ mặc định là cùng một chuỗi. Vì vậy, dòng mã trên đặt cả mật khẩu người dùng và chủ sở hữu

Cuối cùng, ghi tệp PDF được mã hóa vào một tệp đầu ra trong thư mục chính của bạn có tên là

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
964

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
16

Khi bạn mở tệp PDF bằng trình đọc PDF, bạn sẽ được nhắc nhập mật khẩu. Nhập

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
960 để mở PDF

Nếu bạn cần đặt mật khẩu chủ sở hữu riêng cho PDF, hãy chuyển chuỗi thứ hai cho tham số

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
954

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
17

Trong ví dụ này, mật khẩu người dùng là

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
960 và mật khẩu chủ sở hữu là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
968

Khi bạn mã hóa tệp PDF bằng mật khẩu và cố gắng mở tệp đó, bạn phải cung cấp mật khẩu trước khi có thể xem nội dung của tệp. Bảo vệ này mở rộng để đọc từ PDF trong chương trình Python. Tiếp theo, hãy xem cách giải mã tệp PDF bằng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26

Giải mã PDF

Để giải mã tệp PDF được mã hóa, hãy sử dụng phương pháp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
970 của phiên bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
970 có một tham số duy nhất gọi là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
973 mà bạn có thể sử dụng để cung cấp mật khẩu để giải mã. Các đặc quyền bạn có khi mở tệp PDF tùy thuộc vào đối số bạn đã chuyển đến tham số
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
973

Hãy mở tệp

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
964 được mã hóa mà bạn đã tạo trong phần trước và sử dụng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26 để giải mã nó

Đầu tiên, tạo một phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 mới với đường dẫn đến tệp PDF được bảo vệ

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
18

Trước khi bạn giải mã PDF, hãy xem điều gì sẽ xảy ra nếu bạn cố lấy trang đầu tiên

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
19

Một ngoại lệ

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
978 được đưa ra, thông báo cho bạn biết rằng tệp PDF chưa được giải mã

Ghi chú. Tracback ở trên đã được rút ngắn để làm nổi bật những phần quan trọng. Truy nguyên bạn thấy trên máy tính của mình sẽ dài hơn nhiều

Hãy tiếp tục và giải mã tệp ngay bây giờ

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
00

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
970 trả về một số nguyên đại diện cho sự thành công của việc giải mã

  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    69 chỉ ra rằng mật khẩu không chính xác
  • #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    463 chỉ ra rằng mật khẩu người dùng đã được khớp
  • #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    464 chỉ ra rằng mật khẩu chủ sở hữu đã được khớp

Khi bạn đã giải mã tệp, bạn có thể truy cập nội dung của tệp PDF

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
01

Giờ đây, bạn có thể trích xuất văn bản và cắt hoặc xoay trang theo nội dung yêu thích của mình

Kiểm tra việc hiểu của bạn

Mở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn

Tập thể dục. Mã hóa PDFHiển thị/Ẩn

Trong thư mục

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
32 trong kho lưu trữ đồng hành cho bài viết này, có một tệp tên là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
984

Sử dụng

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
985, mã hóa tệp bằng mật khẩu người dùng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
986. Lưu tệp được mã hóa dưới dạng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
987 trong thư mục chính trên máy tính của bạn

Bạn có thể mở rộng khối bên dưới để xem giải pháp

Giải pháp. Mã hóa PDFHiển thị/Ẩn

Thiết lập đường dẫn đến tệp PDF

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
02

Bây giờ hãy tạo các phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
33 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
409

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
10

Bạn có thể nối tất cả các trang từ

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
82 đến
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 bằng cách sử dụng
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
700

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
04

Bây giờ sử dụng _____ 2993 để đặt mật khẩu người dùng thành ________ 2994

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
05

Cuối cùng, viết nội dung của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# create a cell, specify its alignment and position
pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
  
# add another cell
pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
  
# save the pdf with name .pdf
pdf.output["fedingo.pdf"]   
414 vào một tệp có tên
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
987 trong thư mục chính của máy tính của bạn

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
06

Tạo tệp PDF từ đầu

Gói

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26 rất tốt để đọc và sửa đổi các tệp PDF hiện có, nhưng nó có một hạn chế lớn. bạn không thể sử dụng nó để tạo một tệp PDF mới. Trong phần này, bạn sẽ sử dụng Bộ công cụ ReportLab để tạo tệp PDF từ đầu

ReportLab là một giải pháp đầy đủ tính năng để tạo tệp PDF. Có phiên bản thương mại phải trả tiền để sử dụng, nhưng cũng có phiên bản mã nguồn mở có tính năng hạn chế

Ghi chú. Phần này không phải là phần giới thiệu đầy đủ về ReportLab, mà là một ví dụ về những gì có thể

Để biết thêm ví dụ, hãy xem trang đoạn mã của ReportLab

Cài đặt
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998

Để bắt đầu, bạn cần cài đặt

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998 với
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
27

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
07

Bạn có thể xác minh cài đặt với

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
501

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
08

Tại thời điểm viết bài, phiên bản mới nhất của

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998 là 3. 5. 34. Nếu bạn đang mở IDLE, thì bạn cần phải khởi động lại nó trước khi có thể sử dụng gói
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998

Sử dụng lớp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504

Giao diện chính để tạo tệp PDF với

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998 là lớp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504, lớp này nằm trong mô-đun
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
507

Mở một cửa sổ tương tác IDLE mới và nhập thông tin sau để nhập lớp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
09

Khi bạn tạo một phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504 mới, bạn cần cung cấp một chuỗi có tên tệp PDF mà bạn đang tạo. Hãy tiếp tục và tạo một phiên bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504 mới cho tệp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
511

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
10

Bây giờ bạn có một phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504 mà bạn đã gán cho tên biến
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
513 và được liên kết với một tệp trong thư mục làm việc hiện tại của bạn có tên là
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
511. Tuy nhiên, tệp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
511 chưa tồn tại

Hãy thêm một số văn bản vào PDF. Để làm điều đó, bạn sử dụng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
516

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
11

Hai đối số đầu tiên được chuyển đến

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
516 xác định vị trí trên canvas nơi văn bản được viết. Cái đầu tiên chỉ định khoảng cách từ cạnh trái của canvas và cái thứ hai chỉ định khoảng cách từ cạnh dưới cùng

Các giá trị được chuyển đến

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
516 được đo bằng điểm. Vì một điểm bằng 1/72 inch, nên
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
519 vẽ chuỗi
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
520 một inch từ bên trái và một inch từ cuối trang

Để lưu tệp PDF vào một tệp, hãy sử dụng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
521

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
12

Bây giờ bạn có một tệp PDF trong thư mục làm việc hiện tại của mình có tên là

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
511. Bạn có thể mở nó bằng trình đọc PDF và xem văn bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
523 ở cuối trang

Có một số điều cần lưu ý về tệp PDF bạn vừa tạo

  1. Kích thước trang mặc định là A4, không giống với kích thước trang thư tiêu chuẩn của Hoa Kỳ
  2. Phông chữ mặc định là Helvetica với cỡ chữ 12 point

Bạn không bị mắc kẹt với các cài đặt này

Đặt Kích thước Trang

Khi bạn khởi tạo một đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504, bạn có thể thay đổi kích thước trang bằng tham số
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
525 tùy chọn. Tham số này chấp nhận một bộ giá trị dấu phẩy động đại diện cho chiều rộng và chiều cao của trang theo điểm

Ví dụ: để đặt kích thước trang thành chiều rộng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
526 inch x chiều cao
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
527 inch, bạn sẽ tạo
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504 sau

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
13

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
529 đại diện cho một tờ giấy cỡ chữ vì
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
526 nhân với
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
531 là
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
532 và
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
527 nhân với
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
531 là
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
297

Nếu việc tính toán để chuyển đổi điểm sang inch hoặc centimet không phải là sở thích của bạn, thì bạn có thể sử dụng mô-đun ________ 3536 để giúp bạn chuyển đổi. Mô-đun

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
537 chứa một số đối tượng trợ giúp, chẳng hạn như
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
538 và
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
539, giúp đơn giản hóa các chuyển đổi của bạn

Tiếp tục và nhập các đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
538 và
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
539 từ mô-đun
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
536

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
14

Bây giờ bạn có thể kiểm tra từng đối tượng để xem chúng là gì

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
15

Cả

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
539 và
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
538 đều là các giá trị dấu phẩy động. Chúng đại diện cho số điểm có trong mỗi đơn vị.
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
538 là
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
546 điểm và
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
539 là
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
548 điểm

Để sử dụng các đơn vị, hãy nhân tên đơn vị với số lượng đơn vị mà bạn muốn chuyển đổi thành điểm. Ví dụ: đây là cách sử dụng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
538 để đặt kích thước trang thành chiều rộng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
526 inch x chiều cao
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
527 inch

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
16

Bằng cách chuyển một bộ tới

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
525, bạn có thể tạo bất kỳ kích thước trang nào bạn muốn. Tuy nhiên, gói
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998 có một số kích thước trang tích hợp tiêu chuẩn dễ làm việc hơn

Các kích thước trang được đặt trong mô-đun

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
554. Ví dụ: để đặt kích thước trang thành chữ cái, bạn có thể nhập đối tượng
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
555 từ mô-đun
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
556 và chuyển nó tới tham số
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
525 khi khởi tạo
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504 của bạn

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
17

Nếu bạn kiểm tra đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
555, thì bạn sẽ thấy rằng đó là một bộ số float

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
18

Mô-đun

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
560 chứa nhiều kích thước trang tiêu chuẩn. Dưới đây là một số với kích thước của chúng

Kích thước trangKích thước

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
561210 mm x 297 mm
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
5558. 5 trong x 11 trong_______35638. 5 trong x 14 trong________ 356411 trong x 17 trong

Ngoài những thứ này, mô-đun còn chứa các định nghĩa cho tất cả các khổ giấy tiêu chuẩn ISO 216

Đặt thuộc tính phông chữ

Bạn cũng có thể thay đổi phông chữ, cỡ chữ và màu phông chữ khi viết văn bản vào

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504

Để thay đổi phông chữ và cỡ chữ, bạn có thể sử dụng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
566. Đầu tiên, tạo một phiên bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504 mới với tên tệp
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
568 và kích thước trang chữ cái

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
19

Sau đó đặt phông chữ thành Times New Roman với kích thước là

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
569 điểm

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
20

Cuối cùng, viết chuỗi

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
570 vào canvas và lưu nó

>>>

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
21

Với các cài đặt này, văn bản sẽ được viết một inch từ bên trái của trang và mười inch từ dưới cùng. Mở tệp

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
568 trong thư mục làm việc hiện tại của bạn và kiểm tra nó

Có ba phông chữ có sẵn theo mặc định

  1. $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    572
  2. $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    573
  3. $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    574

Mỗi phông chữ có các biến thể in đậm và in nghiêng. Đây là danh sách tất cả các biến thể phông chữ có sẵn trong

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998

  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    572
  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    577
  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    578
  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    579
  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    573
  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    581
  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    582
  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    583
  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    584
  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    585
  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    586
  • $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    574

Bạn cũng có thể đặt màu phông chữ bằng cách sử dụng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
588. Trong ví dụ sau, bạn tạo một tệp PDF có chữ màu xanh tên là
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
589

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
22

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
590 là một đối tượng được nhập từ mô-đun
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
591. Mô-đun này chứa một số màu phổ biến. Có thể tìm thấy danh sách đầy đủ các màu trong mã nguồn
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998

Các ví dụ trong phần này nêu bật những điều cơ bản khi làm việc với đối tượng

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504. Nhưng bạn chỉ trầy xước bề mặt. Với
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998, bạn có thể tạo bảng, biểu mẫu và thậm chí cả đồ họa chất lượng cao từ đầu

Hướng dẫn sử dụng ReportLab chứa rất nhiều ví dụ về cách tạo tài liệu PDF từ đầu. Đó là một nơi tuyệt vời để bắt đầu nếu bạn muốn tìm hiểu thêm về cách tạo tệp PDF bằng Python

Kiểm tra việc hiểu của bạn

Mở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn

Tập thể dục. Tạo PDF từ đầuHiển thị/Ẩn

Tạo một tệp PDF trong thư mục chính trên máy tính của bạn có tên là

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
595 với các trang có kích thước chữ cái chứa văn bản
$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
596 được đặt cách mép trái hai inch và cách mép dưới của trang tám inch

Bạn có thể mở rộng khối bên dưới để xem giải pháp

Giải pháp. Tạo PDF từ đầuHiển thị/Ẩn

Thiết lập phiên bản

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
504 với các trang có kích thước chữ cái

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
23

Bây giờ vẽ chuỗi

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
596 hai inch từ bên trái và tám inch từ phía dưới

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
24

Cuối cùng, lưu

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
513 để ghi tệp PDF

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
25

Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo

Sự kết luận. Tạo và sửa đổi tệp PDF bằng Python

Trong hướng dẫn này, bạn đã học cách tạo và sửa đổi tệp PDF bằng gói

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26 và
#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998. Nếu bạn muốn làm theo các ví dụ bạn vừa xem, thì hãy nhớ tải xuống tài liệu bằng cách nhấp vào liên kết bên dưới

Tải tài liệu mẫu. Nhấp vào đây để lấy tài liệu bạn sẽ sử dụng để tìm hiểu về cách tạo và sửa đổi tệp PDF trong hướng dẫn này

Với

$ python3 -m pip show PyPDF2
Name: PyPDF2
Version: 1.26.0
Summary: PDF toolkit
Home-page: //mstamy2.github.com/PyPDF2
Author: Mathieu Fenniak
Author-email: biziqe@mathieu.fenniak.net
License: UNKNOWN
Location: c:\\users\\david\\python38-32\\lib\\site-packages
Requires:
Required-by:
26, bạn đã học cách

  • Đọc tệp PDF và trích xuất văn bản bằng lớp
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    33
  • Viết tệp PDF mới bằng lớp
    #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    409
  • Nối và hợp nhất các tệp PDF bằng lớp
    #import fpdf
    from fpdf import FPDF
       
    #create object instance
    pdf = FPDF[]
      
    #add page
    pdf.add_page[]
      
    # set font style and size
    pdf.set_font["Arial", size = 15]
      
    # create a cell, specify its alignment and position
    pdf.cell[200, 10, txt = "Fedingo.com", ln = 1, align = 'C']
      
    # add another cell
    pdf.cell[200, 10, txt = "Useful tech articles", ln = 2, align = 'C']
      
    # save the pdf with name .pdf
    pdf.output["fedingo.pdf"]   
    720
  • Xoay và cắt các trang PDF
  • Mã hóa và giải mã các tệp PDF bằng mật khẩu

Bạn cũng đã có phần giới thiệu về cách tạo tệp PDF từ đầu với gói

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998. Bạn đã học cách

  • Sử dụng lớp
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    504
  • Viết văn bản cho một
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    504 với
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    516
  • Đặt phông chữ và cỡ chữ bằng
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    566
  • Thay đổi màu phông chữ bằng
    $ python3 -m pip show PyPDF2
    Name: PyPDF2
    Version: 1.26.0
    Summary: PDF toolkit
    Home-page: //mstamy2.github.com/PyPDF2
    Author: Mathieu Fenniak
    Author-email: biziqe@mathieu.fenniak.net
    License: UNKNOWN
    Location: c:\\users\\david\\python38-32\\lib\\site-packages
    Requires:
    Required-by:
    
    588

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF[]
  
#add page
pdf.add_page[]
  
# set font style and size
pdf.set_font["Arial", size = 15]
  
# open the text file in read mode
f = open["data.txt", "r"]
 
# insert the texts in pdf
for x in f:
       pdf.cell[200, 10, txt = x, ln = 1, align = 'C']
 
  
# save the pdf with name .pdf
pdf.output["data.pdf"]   
998 là một công cụ tạo PDF mạnh mẽ và bạn mới chỉ vạch ra bề nổi của những gì có thể. Nếu bạn thích những gì bạn học được trong ví dụ này từ Python Basics. Giới thiệu thực tế về Python 3, thì hãy nhớ xem phần còn lại của cuốn sách

Mã hóa vui vẻ

Đánh dấu là đã hoàn thành

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Gửi cho tôi thủ thuật Python »

Giới thiệu về David Amos

David là một nhà văn, lập trình viên và nhà toán học đam mê khám phá toán học thông qua mã

» Thông tin thêm về David

Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Aldren

Joanna

Gia-cốp

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi

Chủ Đề