Hướng dẫn plotting pdf in python - âm mưu pdf trong python

Nó thực sự hữu ích để biết cách tạo và sửa đổi các tệp PDF trong Python. PDF, hoặc đị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. PDF có thể chứa văn bản, hình ảnh, bảng, biểu mẫu và phương tiện phong phú như video và hình ảnh động, tất cả trong một tệp.PDF, or Portable Document Format, is one of the most common formats for sharing documents over the Internet. PDFs can contain text, images, tables, forms, and rich media like videos and animations, all in a single file.

Nội phân chính

  • Ghép và sáp nhập các tệp PDF
  • Sử dụng lớp pdffilemerger
  • Ghép các tệp PDF với .Append []
  • Hợp nhất PDFS với .merge []
  • Kiểm tra việc hiểu của bạn
  • Xoay và cắt các trang PDF
  • Trang quay
  • Trang cắt xén
  • Kiểm tra việc hiểu của bạn
  • Xoay và cắt các trang PDF
  • Trang quay
  • Trang cắt xén
  • Kiểm tra việc hiểu của bạn
  • Xoay và cắt các trang PDF
  • Trang quay
  • Trang cắt xén
  • Mã hóa và giải mã PDFS
  • Mã hóa PDFS
  • Kiểm tra việc hiểu của bạn
  • Xoay và cắt các trang PDF

Trang quay

Trang cắt xén

  • Mã hóa và giải mã PDFS text from a PDF
  • Mã hóa PDFS a PDF into multiple files
  • Giải mã PDFS and merge PDF files
  • Tạo tệp PDF từ đầu and crop pages in a PDF file
  • Cài đặt Báo cáo and decrypt PDF files with passwords
  • Sử dụng lớp Canvas a PDF file from scratch

Đặt kích thước trang

Ghép và sáp nhập các tệp PDF

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

Kết luận: Tạo và sửa đổi các tệp PDF trong Pythonconcatenate two or more PDFs, you join the files one after another into a single document. For example, a company may concatenate several daily reports into one monthly report at the end of a month.

Sự phong phú của các loại nội dung này có thể làm cho việc làm việc với PDF trở nên khó khăn. Có rất nhiều loại dữ liệu khác nhau để giải mã khi mở tệp 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 các tệp PDF. two PDFs also joins the PDFs into a single file. But instead of joining the second PDF to the end of the first, merging allows you to insert it after a specific page in the first PDF. Then it pushes all of the first PDF’s pages after the insertion point to the end of the second PDF.

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

Sử dụng lớp pdffilemerger

Đọc văn bản từ PDF

Chia PDF thành nhiều tệp

Concatenate và hợp nhất các tệp PDF

Xoay và cắt các trang trong tệp PDF

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]

Mã hóa và giải mã các tệp PDF bằng mật khẩu

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

  • Trên đường đi, bạn sẽ có một số cơ hội để hiểu sâu hơn về sự hiểu biết của bạn bằng cách theo dõi cùng với 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: concatenates every page in an existing PDF document to the end of the pages currently in the
    >>> from pathlib import Path
    >>> from PyPDF2 import PdfFileMerger
    >>> report_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "quarterly_report"
    ... ]
    >>> report_path = report_dir / "report.pdf"
    >>> toc_path = report_dir / "toc.pdf"
    
    3.
  • Hai tác vụ phổ biến khi làm việc với các tệp PDF đang kết hợp và hợp nhất một số tệp PDF thành một tệp. inserts all of the pages in an existing PDF document after a specific page in the
    >>> from pathlib import Path
    >>> from PyPDF2 import PdfFileMerger
    >>> report_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "quarterly_report"
    ... ]
    >>> report_path = report_dir / "report.pdf"
    >>> toc_path = report_dir / "toc.pdf"
    
    3.

Khi bạn kết hợp hai hoặc nhiều tệp PDF, bạn tham gia các tệp này lần lượt vào một tài liệu. Ví dụ, một công ty có thể kết hợp một số báo cáo hàng ngày vào một báo cáo hàng tháng vào cuối một tháng.

Ghép các tệp PDF với .Append []

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

Trong phần này, bạn sẽ học cách kết hợp và hợp nhất các tệp PDF bằng cách sử dụng gói

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
3.

Lớp

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
3 rất giống với lớp
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5 mà bạn đã học trong phần trước. Bạn có thể sử dụng cả hai lớp để viết các tệp PDF. Trong cả hai trường hợp, bạn thêm các trang vào các phiên bản của lớp và sau đó viết chúng vào một tệp.

Xoay và cắt các trang trong tệp PDF

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]

Mã hóa và giải mã các tệp PDF bằng mật khẩu

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

Trên đường đi, bạn sẽ có một số cơ hội để hiểu sâu hơn về sự hiểu biết của bạn bằng cách theo dõi cùng với 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:

Xoay và cắt các trang trong tệp PDF

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf

Mã hóa và giải mã các tệp PDF bằng mật khẩu

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
06 không được đảm bảo, vì vậy bạn sẽ cần phải tự đặt hàng 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
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
08 trong danh sách đó:

>>>

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]

Hãy nhớ rằng

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
08 sắp xếp một danh sách tại chỗ, vì vậy bạn không cần phải gán giá trị trả về cho một biến. Danh sách
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
8 sẽ được sắp xếp theo thứ tự bảng chữ cái theo tên tệp sau khi
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
11 được gọi.

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

>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
8 và in ra các tên tệp:

>>>

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf

Hãy nhớ rằng

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
08 sắp xếp một danh sách tại chỗ, vì vậy bạn không cần phải gán giá trị trả về cho một biến. Danh sách
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
8 sẽ được sắp xếp theo thứ tự bảng chữ cái theo tên tệp sau khi
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
11 được gọi.

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

>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
8 và in ra các tên tệp:

Trông tôt đây!

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]

Hãy nhớ rằng

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
08 sắp xếp một danh sách tại chỗ, vì vậy bạn không cần phải gán giá trị trả về cho một biến. Danh sách
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
8 sẽ được sắp xếp theo thứ tự bảng chữ cái theo tên tệp sau khi
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
11 được gọi.

>>>

>>> for path in expense_reports:
...     pdf_merger.append[str[path]]
...

Hãy nhớ rằng

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
08 sắp xếp một danh sách tại chỗ, vì vậy bạn không cần phải gán giá trị trả về cho một biến. Danh sách
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
8 sẽ được sắp xếp theo thứ tự bảng chữ cái theo tên tệp sau khi
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
11 được gọi.

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

>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
8 và in ra các tên tệp:

Trông tôt đây!

>>>

>>> with Path["expense_reports.pdf"].open[mode="wb"] as output_file:
...     pdf_merger.write[output_file]
...

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
13, yêu cầu một đối số chuỗi duy nhất đại diện cho đường dẫn đến tệp PDF. Khi bạn gọi
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
2, tất cả các trang trong tệp PDF được nối vào tập hợp các trang trong đối tượng
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
3.

Hãy cùng xem điều này trong hành động. Đầu tiên, nhập lớp
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
3 và tạo một thể hiện mới:

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

>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
8 được sắp xếp và nối chúng vào
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
1:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 trong
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
01 được chuyển đổi thành một chuỗi với
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
21 trước khi được chuyển sang
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
22.

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
01 được nối với nhau trong đối tượng
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
1, điều cuối cùng bạn cần làm là viết mọi thứ vào tệp PDF đầu ra.
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
3 Các trường hợp có phương thức
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
26 hoạt động giống như
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
27.

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
28:

>>>

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"

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 bạn có tên là

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
29. Mở nó với đầu đọc PDF và bạn sẽ tìm thấy cả ba báo cáo chi phí cùng nhau trong cùng một tệp PDF.

>>>

>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]

Hợp nhất PDFS với .merge []

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
30. Phương pháp này tương tự như
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
2, ngoại trừ việc bạn phải chỉ định vị trí trong PDF đầu ra để chèn tất cả nội dung từ PDF bạn đang hợp nhất.

Hãy xem một ví dụ. Goggle, Inc. đã chuẩn bị một báo cáo hàng quý nhưng quên bao gồm một bảng nội dung. Peter Python nhận thấy sai lầm và nhanh chóng tạo ra một tệp PDF với mục lục nội dung bị thiếu. Bây giờ anh ta cần phải hợp nhất pdf đó vào báo cáo ban đầu.

  1. Cả PDF báo cáo và bảng nội dung PDF đều có thể được tìm thấy trong thư mục con
    >>> from pathlib import Path
    >>> reports_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... ]
    
    32 của thư mục
    >>> from pathlib import Path
    >>> reports_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... ]
    
    33. Báo cáo có trong một tệp có tên
    >>> from pathlib import Path
    >>> reports_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... ]
    
    34 và bảng nội dung nằm trong một tệp có tên
    >>> from pathlib import Path
    >>> reports_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... ]
    
    35.
  2. Trong cửa sổ tương tác Idle, nhập lớp
    >>> from pathlib import Path
    >>> from PyPDF2 import PdfFileMerger
    >>> report_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "quarterly_report"
    ... ]
    >>> report_path = report_dir / "report.pdf"
    >>> toc_path = report_dir / "toc.pdf"
    
    3 và tạo các đối tượng
    >>> from pathlib import Path
    >>> reports_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... ]
    
    00 cho các tệp
    >>> from pathlib import Path
    >>> reports_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... ]
    
    34 và
    >>> from pathlib import Path
    >>> reports_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... ]
    
    35:

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

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
3 mới bằng cách sử dụng
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
2:

>>>

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
0

Bây giờ

>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
1 có một số trang trong đó, bạn có thể hợp nhất bảng nội dung PDF vào nó tại vị trí chính xác. Nếu bạn mở tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
34 với đầu đọc PDF, thì bạn sẽ thấy trang đầu tiên của báo cáo là một trang tiêu đề. 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 bảng nội dung sau trang tiêu đề và ngay trước phần giới thiệu. Vì các chỉ số trang PDF bắt đầu với

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
44 trong
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2, bạn cần chèn bảng nội dung sau trang tại INDEX
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
44 và trước trang tại Index
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
47.

>>>

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
1

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
48 với hai đối số:

Số nguyên

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
47, chỉ ra chỉ mục của trang mà bảng nội dung nên được chèn

Một chuỗi chứa đường dẫn của tệp pdf cho mục lục

Ở đây, những gì trông giống như:

Mỗi trang trong bảng nội dung PDF được chèn trước trang tại Index

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
47. Vì bảng nội dung PDF chỉ là một trang, nó được chèn vào chỉ mục
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
47. Trang hiện tại tại Index
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
47 sau đó được chuyển sang Index
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
53. Trang hiện đang ở Index
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
53 được chuyển sang Index
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
55, v.v.

Sử dụng thể hiện

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
60, kết hợp hai tệp bằng
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
2. Lưu các tệp PDF được nối với một tệp mới có tên
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
62 nếu thư mục nhà 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:

Đặt đường dẫn đến tệp PDF:

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
2

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

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
3:

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
3

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
64 và
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
2 mỗi tệp thành
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
1:

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
4

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

>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
1 vào một tệp có tên
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
62 trong thư mục nhà của bạn:

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
5

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

Xoay và cắt các trang PDF

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

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2 có nhiều tính năng hữu ích khác.

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

Trang quay

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70 trong thư mục
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
33. Tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70 chứa một phiên bản đáng yêu của Hans Christian Andersen, con vịt xấu xí, ngoại trừ mọi trang được đánh số lẻ đều được xoay ngược chiều kim đồng hồ.

Hãy để sửa chữa điều đó. Trong một cửa sổ tương tác mới nhàn rỗi, hãy bắt đầu bằng cách nhập các lớp

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 và
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5 từ
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2, cũng như lớp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 từ mô -đun
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
9:

>>>

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
6

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 cho tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70:

>>>

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
7

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 cho tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70:

>>>

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
8

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 cho tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 và
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5 mới:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82 để tạo tệp PDF mới trong đó tất cả các trang đều có định hướng chính xác. Các trang được đánh số chẵn trong PDF đã được định hướng đúng, nhưng các trang được đánh số lẻ được xoay ngược chiều kim đồng hồ bởi chín mươi độ.

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
83. Phương pháp này có một đối số số nguyên, tính theo độ và xoay một trang theo chiều kim đồng hồ theo nhiều độ đó. Ví dụ,
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
84 xoay một trang PDF theo chiều kim đồng hồ theo chín mươi độ.

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
85, nhưng họ thực hiện các cách tiếp cận khác nhau để xác định trang nào được xoay.

>>>

Bây giờ tạo một đối tượng
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 cho tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 và
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5 mới:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82 để tạo tệp PDF mới trong đó tất cả các trang đều có định hướng chính xác. Các trang được đánh số chẵn trong PDF đã được định hướng đúng, nhưng các trang được đánh số lẻ được xoay ngược chiều kim đồng hồ bởi chín mươi độ.

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
83. Phương pháp này có một đối số số nguyên, tính theo độ và xoay một trang theo chiều kim đồng hồ theo nhiều độ đó. Ví dụ,
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
84 xoay một trang PDF theo chiều kim đồng hồ theo chín mươi độ.

>>>

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
0

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 cho tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 và
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5 mới:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82 để tạo tệp PDF mới trong đó tất cả các trang đều có định hướng chính xác. Các trang được đánh số chẵn trong PDF đã được định hướng đúng, nhưng các trang được đánh số lẻ được xoay ngược chiều kim đồng hồ bởi chín mươi độ.

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
83. Phương pháp này có một đối số số nguyên, tính theo độ và xoay một trang theo chiều kim đồng hồ theo nhiều độ đó. Ví dụ,
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
84 xoay một trang PDF theo chiều kim đồng hồ theo chín mươi độ.

>>>

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
1

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 cho tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 và
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5 mới:

>>>

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
2

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 cho tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 và
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5 mới:

>>>

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
3

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 cho tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70:

>>>

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
4

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 cho tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70:

Nếu bạn xoay trang đầu tiên bằng

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
85, thì giá trị của
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
97 thay đổi từ
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
98 thành
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
44:

>>>

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
5

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
97, bạn có thể sử dụng nó để xoay các trang trong tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70.

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
03 và
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82 để bạn có một khởi đầu mới:

>>>

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
8

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
97, bạn có thể sử dụng nó để xoay các trang trong tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70.

>>>

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
7

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
97, bạn có thể sử dụng nó để xoay các trang trong tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70.

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
03 và
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82 để bạn có một khởi đầu mới:

>>>

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
8

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
97, bạn có thể sử dụng nó để xoay các trang trong tệp
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
70.

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
03 và
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82 để bạn có một khởi đầu mới:

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
17 có thể sử dụng được, kiểm tra giá trị của
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
97 và xoay trang nếu giá trị đó là
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
98:

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à nó không dựa vào bất kỳ kiến ​​thức nào trước đây về các 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ỳ PDF nào mà không cần phải mở nó ra và nhìn vào nó.

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82 vào một tệp mới:

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
21 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
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
91 bạn đã tạo trước đó. Họ nên trông giống hệt nhau.

Giá trị của

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
97 có thể không phải lúc nào cũng là những gì bạn mong đợi. Ví dụ: nếu bạn quét một tài liệu giấy với trang xoay chín mươi độ ngược chiều kim đồng hồ, thì nội dung của PDF sẽ xuất hiện xoay. Tuy nhiên, khóa
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
97 có thể có giá trị
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
44.

Đây là một trong nhiều quirks có thể làm cho các tệp PDF bực bội. Đôi khi, bạn chỉ cần mở PDF trong chương trình đầu đọc PDF và tìm ra mọi thứ theo cách thủ công.

>>>

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
6

Trang cắt xén

>>>

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
0

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

>>>

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
1

Ví dụ: thư mục

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
33 bao gồm một tệp có tên
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
27. PDF này chứa một phần của Hans Christian Andersen, The Little Mermaid.

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

>>>

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
2

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 và
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5 từ lớp
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2 và
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 từ mô -đun
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
9:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
00 cho tệp
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
27:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 mới và nhận trang đầu tiên của PDF:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
96 Các trường hợp như
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
37 có thuộc tính
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
38 đạ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 Idle, để khám phá

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
38 trước khi sử dụng nó cắt trang:

>>>

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
3

Thuộc tính

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
38 trả về
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
41. Đối tượng này được xác định trong gói
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2 và đại diện cho một khu vực hình chữ nhật trên trang.

>>>

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
4

Danh sách

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
43 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. Các số thứ ba và thứ tư đại diện cho chiều rộng và chiều cao của hình chữ nhật, tương ứng. Các đơn vị của tất cả các giá trị là các điểm, bằng 1/72 inch.

>>>

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
5

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
44 đại diện cho một vùng hình chữ nhật có góc dưới bên trái ở gốc, chiều rộng
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
45 điểm, hoặc 11 inch, và chiều cao 612 điểm, hoặc 8,5 inch. Đó là các kích thước của một trang có kích thước chữ cái tiêu chuẩn theo định hướng cảnh quan, được sử dụng cho ví dụ PDF của Nàng tiên cá nhỏ. Một trang PDF có kích thước chữ cái theo hướng chân dung sẽ trả về đầu ra
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
46.

>>>

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
6

A

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
41 có bốn thuộc tính trả về tọa độ của các góc hình chữ nhật:
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
48,
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
49,
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
50 và
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
51. Giống như các giá trị chiều rộng và chiều cao, các tọa độ này được đưa ra theo điểm.

Bạn có thể sử dụng bốn thuộc tính này để có được tọa độ của mỗi góc của

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
41:

>>>

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
7

Nếu bạn mở tệp

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
61 trong thư mục làm việc hiện tại của mình, thì bạn sẽ thấy phần trên cùng của trang đã bị xóa.

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
51 của đối tượng
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
38. Hãy để xem cách hoạt động của nó.

Đầu tiên, bạn cần nhận các đối tượng

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 và
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5 mới vì bạn đã thay đổi trang đầu tiên trong
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
03 và thêm nó vào
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82:

>>>

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
8

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

>>>

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
9

Lần này, hãy để Lừa 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 còn nguyên vẹn. Bạn có thể làm điều đó bằng cách nhập mô -đun

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
68 từ thư viện tiêu chuẩn Python và sử dụng
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
69 để tạo một bản sao của trang:

>>>

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
0

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
70 mà không thay đổi thuộc tính của
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
37. Bằng cách đó, bạn có thể sử dụng
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
37 sau để trích xuất văn bản ở phía bên phải của trang.

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
38 sang trung tâm trên cùng của trang. Để làm điều đó, bạn sẽ tạo một
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
53 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
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
51.

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
38.

>>>

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
1

Sau đó, tạo một

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
53 mới có tọa độ đầu tiên là một nửa giá trị của tọa độ hiện tại và tọa độ thứ hai giống như bản gốc:

>>>

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
2

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
51:

>>>

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
3

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

Đầu tiên nhận được một bản sao mới của

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
37:

>>>

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
4

Di chuyển góc

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
50 thay vì góc
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
51:

>>>

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
5

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
82 hiện là một hình chữ nhật có góc trên bên trái nằm ở trung tâm trên cùng của trang và có góc trên bên phải ở trên cùng bên phải của trang.

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
70 và
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
84 vào
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82 và viết chúng vào tệp PDF mới:

>>>

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
6

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
86 với đầu đọc PDF. Bạn sẽ thấy một tệp có hai trang, phần đầu tiên chứa văn bản từ phía bên trái của trang đầu tiên gốc và trang thứ hai chứa văn bản từ phía bên phải ban đầu.

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:

Trong thư mục

>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
7 trong kho lưu trữ đồng hành cho bài viết này, có một tệp gọi là
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
88.

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
89 trong thư mục nhà máy tính của bạn có chứa tất cả các trang từ
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
88, 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:

Đặt đường dẫn đến tệp PDF:

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
7

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 và
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5:

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
8

Vòng lặp qua các trang trong

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
03, xoay tất cả chúng bằng 90 xuống bằng cách sử dụng
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
94 và thêm chúng vào
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82:

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
9

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82 vào một tệp có tên
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
89 trong thư mục nhà máy tính của bạn:

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]
0

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

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

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2, bạn có thể làm việc với các tệp PDF được mã hóa cũng như thêm bảo vệ mật khẩu vào các tệp PDF hiện có.

Mã hóa PDFS

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
99 của phiên bản
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
00. Nó có hai tham số chính:

  1. >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    01 đặt mật khẩu người dùng. Điều này cho phép mở và đọc tệp PDF.
    sets the user password. This allows for opening and reading the PDF file.
  2. >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    02 Đặt mật khẩu của chủ sở hữu. Điều này cho phép mở PDF mà không có bất kỳ hạn chế nào, bao gồm chỉnh sửa.
    sets the owner password. This allows for opening the PDF without any restrictions, including editing.

Hãy để sử dụng

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
99 để thêm mật khẩu vào tệp PDF. Đầu tiên, hãy mở tệp
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
04 trong thư mục
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
33:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]
1

Bây giờ hãy tạo một thể hiện

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5 mới và thêm các trang từ
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
03 vào nó:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]
2

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
08 với
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
09:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]
3

Khi bạn chỉ đặt

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
01, đối số
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
02 mặc định cho cùng một chuỗi. Vì vậy, dòng mã trên đặt cả mật khẩu của người dùng và chủ sở hữu.

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
12:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]
4

Khi bạn mở tệp PDF với đầu đọc PDF, bạn sẽ được nhắc nhập mật khẩu. Nhập

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
08 để mở PDF.

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
02:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]
5

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
08 và mật khẩu chủ sở hữu là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
16.

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

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2.

Giải mã PDFS

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
18 của một thể hiện
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73.

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
18 có một tham số duy nhất được gọi là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
21 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ở PDF phụ thuộc vào đối số bạn đã chuyển cho tham số
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
21.

Hãy để mở tệp

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
12 được mã hóa mà bạn đã tạo trong phần trước và sử dụng
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2 để giải mã nó.

Đầu tiên, hãy tạo một thể hiện

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 mới với đường dẫn đến PDF được bảo vệ:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]
6

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
08 và mật khẩu chủ sở hữu là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
16.

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]
7

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
08 và mật khẩu chủ sở hữu là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
16.

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

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2.

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]
8

Giải mã PDFS

  • Để giải mã một tệp PDF được mã hóa, hãy sử dụng phương thức
    >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    18 của một thể hiện
    >>> from pathlib import Path
    >>> reports_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... ]
    
    73.
    indicates that the password is incorrect.
  • >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    18 có một tham số duy nhất được gọi là
    >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    21 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ở PDF phụ thuộc vào đối số bạn đã chuyển cho tham số
    >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    21.
    indicates that the user password was matched.
  • Hãy để mở tệp
    >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    12 được mã hóa mà bạn đã tạo trong phần trước và sử dụng
    >>> from pathlib import Path
    >>> from PyPDF2 import PdfFileMerger
    >>> report_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "quarterly_report"
    ... ]
    >>> report_path = report_dir / "report.pdf"
    >>> toc_path = report_dir / "toc.pdf"
    
    2 để giải mã nó.
    indicates that the owner password was matched.

Đầu tiên, hãy tạo một thể hiện

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 mới với đường dẫn đến PDF được bảo vệ:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger[]
9

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

Một ngoại lệ
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
26 được nêu ra, thông báo cho bạn rằng tệp PDF chưa được giải mã.

Đi trước và giải mã tập tin ngay bây giờ:

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
18 Trả về một số nguyên đại diện cho sự thành công của giải mã:

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
44 chỉ ra rằng mật khẩu không chính xác.

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
47 chỉ ra rằng mật khẩu người dùng được khớp.

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
53 chỉ ra rằng mật khẩu của chủ sở hữu đã được khớp.

>>> for path in expense_reports:
...     pdf_merger.append[str[path]]
...
0

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

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
8

Bây giờ bạn có thể trích xuất văn bản và cắt hoặc xoay trang đến nội dung trái tim của bạn!

>>> for path in expense_reports:
...     pdf_merger.append[str[path]]
...
2

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

>>> for path in expense_reports:
...     pdf_merger.append[str[path]]
...
3

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

>>> for path in expense_reports:
...     pdf_merger.append[str[path]]
...
4

Trong thư mục
>>> pdf_merger = PdfFileMerger[]
>>> pdf_merger.append[str[report_path]]
7 trong kho lưu trữ đồng hành cho bài viết này, có một tệp gọi là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
32.

Sử dụng

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
33, mã hóa tệp bằng mật khẩu người dùng
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
34. Lưu tệp được mã hóa dưới dạng
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
35 trong thư mục nhà 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:

Đặt đường dẫn đến tệp PDF:

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

>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
73 và
>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
5:

>>> for path in expense_reports:
...     pdf_merger.append[str[path]]
...
5

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

>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
03 đến
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82 bằng cách sử dụng
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
40:

>>> for path in expense_reports:
...     pdf_merger.append[str[path]]
...
6

Bây giờ sử dụng

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
41 để đặt người dùng Passwrod thành
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
42:

Cuối cùng, hãy viết nội dung của
>>> from pathlib import Path
>>> reports_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... ]
82 vào một tệp có tên
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
35 trong thư mục nhà máy tính của bạn:

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

Gói

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2 rất tốt để đọc và sửa đổi các tệp PDF hiện có, nhưng nó có một giới hạn lớn: bạn có thể sử dụng nó để tạo tệp PDF mới. Trong phần này, bạn sẽ sử dụng bộ công cụ báo cáo để tạo các tệp PDF từ đầu.

>>>

>>> for path in expense_reports:
...     pdf_merger.append[str[path]]
...
7

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

>>>

>>> for path in expense_reports:
...     pdf_merger.append[str[path]]
...
8

Cài đặt Báo cáo

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
46 với
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
47:

>>>

>>> for path in expense_reports:
...     pdf_merger.append[str[path]]
...
9

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
48:

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
46 là 3.5.34. Nếu bạn mở nhàn rỗi, thì bạn sẽ cần phải khởi động lại trước khi bạn có thể sử dụng gói
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
46.

Sử dụng lớp Canvas

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
46 là lớp
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
52, nằm trong mô -đun
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
53.

Có một vài điều cần chú ý về 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 kích thước phông chữ 12 điểm.

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
52, bạn có thể thay đổi kích thước trang bằng tham số
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
71 tùy chọn. Tham số này chấp nhận một bộ các giá trị dấu phẩy động đại diện cho chiều rộng và chiều cao của trang tính theo điểm.

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
72 inch cao bằng
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
73 inch, bạn sẽ tạo ra
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
52 sau đây:

>>> with Path["expense_reports.pdf"].open[mode="wb"] as output_file:
...     pdf_merger.write[output_file]
...
0

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
75 đại diện cho một bài báo cỡ chữ vì
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
72 lần
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
77 là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
78 và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
73 lần
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
77 là
>>> for path in reports_dir.glob["*.pdf"]:
...     print[path.name]
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
45.

Nếu làm toán để chuyển đổi điểm thành inch hoặc centimet không phải là tách trà của bạn, thì bạn có thể sử dụng mô -đun

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
82 để giúp bạn chuyển đổi. Mô -đun
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
83 chứa một số đối tượng trợ giúp, chẳng hạn như
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
84 và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
85, đơn giản hóa các chuyển đổi của bạn.

Đi trước và nhập các đối tượng

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
84 và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
85 từ mô -đun
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
82:

>>>

>>> with Path["expense_reports.pdf"].open[mode="wb"] as output_file:
...     pdf_merger.write[output_file]
...
1

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

>>>

>>> with Path["expense_reports.pdf"].open[mode="wb"] as output_file:
...     pdf_merger.write[output_file]
...
2

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

Cả

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
85 và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
84 đều là các giá trị điểm nổi. Chúng đại diện cho số lượng điểm có trong mỗi đơn vị.
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
84 là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
92 điểm và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
85 là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
94 điểm.

>>>

>>> with Path["expense_reports.pdf"].open[mode="wb"] as output_file:
...     pdf_merger.write[output_file]
...
3

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

Cả

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
85 và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
84 đều là các giá trị điểm nổi. Chúng đại diện cho số lượng điểm có trong mỗi đơn vị.
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
84 là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
92 điểm và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
85 là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
94 điểm.

>>>

>>> with Path["expense_reports.pdf"].open[mode="wb"] as output_file:
...     pdf_merger.write[output_file]
...
4

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

>>>

>>> with Path["expense_reports.pdf"].open[mode="wb"] as output_file:
...     pdf_merger.write[output_file]
...
5

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

Cả
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
85 và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
84 đều là các giá trị điểm nổi. Chúng đại diện cho số lượng điểm có trong mỗi đơn vị.
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
84 là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
92 điểm và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
85 là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
94 điểm.Để sử dụng các đơn vị, 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, cách sử dụng
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
84 để đặt kích thước trang thành rộng ____372 inch bằng
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
73 inch cao:
Bằng cách chuyển một tuple cho
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
71, bạn có thể tạo bất kỳ kích thước nào của trang mà bạn muốn. Tuy nhiên, gói
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
46 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
>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
00. Ví dụ: để đặt kích thước trang thành chữ cái, bạn có thể nhập đối tượng
>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
01 từ mô -đun
>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
02 và chuyển nó vào tham số
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
71 khi khởi tạo
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
52 của bạn:
Nếu bạn kiểm tra đối tượng
>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
01, thì bạn sẽ thấy rằng nó là một bộ nổi của phao:
Mô -đun
>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
06 chứa nhiều kích thước trang tiêu chuẩn. Dưới đây là một số ít với kích thước của họ:
Kích thước trangKích thước
>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
07
210 mm x 297 mm

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
01

8,5 trong x 11 in

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
09

8,5 trong x 14 in

>>>

>>> with Path["expense_reports.pdf"].open[mode="wb"] as output_file:
...     pdf_merger.write[output_file]
...
6

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

>>>

>>> with Path["expense_reports.pdf"].open[mode="wb"] as output_file:
...     pdf_merger.write[output_file]
...
7

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

>>>

>>> with Path["expense_reports.pdf"].open[mode="wb"] as output_file:
...     pdf_merger.write[output_file]
...
8

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

Cả

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
85 và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
84 đều là các giá trị điểm nổi. Chúng đại diện cho số lượng điểm có trong mỗi đơn vị.
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
84 là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
92 điểm và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
85 là
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
94 điểm.

  1. >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    18
  2. Để sử dụng các đơn vị, 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, cách sử dụng
    >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    84 để đặt kích thước trang thành rộng ____372 inch bằng
    >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    73 inch cao:
  3. >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    20

Bằng cách chuyển một tuple cho

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
71, bạn có thể tạo bất kỳ kích thước nào của trang mà bạn muốn. Tuy nhiên, gói
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
46 có một số kích thước trang tích hợp tiêu chuẩn dễ làm việc hơn.

  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    18
  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    23
  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    24
  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    25
  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    19
  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    27
  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    28
  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    29
  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    30
  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    31
  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    32
  • >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    20

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

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
00. Ví dụ: để đặt kích thước trang thành chữ cái, bạn có thể nhập đối tượng
>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
01 từ mô -đun
>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
02 và chuyển nó vào tham số
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
71 khi khởi tạo
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
52 của bạn:

>>> with Path["expense_reports.pdf"].open[mode="wb"] as output_file:
...     pdf_merger.write[output_file]
...
9

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

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
01, thì bạn sẽ thấy rằng nó là một bộ nổi của phao:

Mô -đun

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
06 chứa nhiều kích thước trang tiêu chuẩn. Dưới đây là một số ít với kích thước của họ:

Kích thước trang

Kích thước

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
07

210 mm x 297 mm

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
01

Thiết lập thể hiện

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
52 với các trang có kích thước chữ cái:

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
0

Bây giờ vẽ chuỗi

>>> for path in expense_reports:
...     print[path.name]
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
42 hai inch từ bên trái và tám inch từ phía dưới:

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
1

Cuối cùng, hãy lưu

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
59 để viết tệp PDF:

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

Kết luận: Tạo và sửa đổi các tệp PDF trong Python

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

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2 và
>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
46. Nếu bạn muốn theo dõi cùng với các ví dụ bạn vừa thấy, thì hãy chắc chắn tải xuống các tài liệu bằng cách nhấp vào liên kết bên dưới:

Với

>>> from pathlib import Path
>>> from PyPDF2 import PdfFileMerger
>>> report_dir = [
...     Path.home[]
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "quarterly_report"
... ]
>>> report_path = report_dir / "report.pdf"
>>> toc_path = report_dir / "toc.pdf"
2, bạn đã học được cách:

  • Đọc các tệp PDF và trích xuất văn bản bằng lớp
    >>> from pathlib import Path
    >>> reports_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... ]
    
    73
    PDF files and extract text using the
    >>> from pathlib import Path
    >>> reports_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... ]
    
    73 class
  • Viết các tệp PDF mới bằng lớp
    >>> from pathlib import Path
    >>> from PyPDF2 import PdfFileMerger
    >>> report_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "quarterly_report"
    ... ]
    >>> report_path = report_dir / "report.pdf"
    >>> toc_path = report_dir / "toc.pdf"
    
    5
    new PDF files using the
    >>> from pathlib import Path
    >>> from PyPDF2 import PdfFileMerger
    >>> report_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "quarterly_report"
    ... ]
    >>> report_path = report_dir / "report.pdf"
    >>> toc_path = report_dir / "toc.pdf"
    
    5 class
  • Concatenate và hợp nhất các tệp PDF bằng lớp
    >>> from pathlib import Path
    >>> from PyPDF2 import PdfFileMerger
    >>> report_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "quarterly_report"
    ... ]
    >>> report_path = report_dir / "report.pdf"
    >>> toc_path = report_dir / "toc.pdf"
    
    3
    and merge PDF files using the
    >>> from pathlib import Path
    >>> from PyPDF2 import PdfFileMerger
    >>> report_dir = [
    ...     Path.home[]
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "quarterly_report"
    ... ]
    >>> report_path = report_dir / "report.pdf"
    >>> toc_path = report_dir / "toc.pdf"
    
    3 class
  • Xoay và cắt các trang PDF and crop PDF pages
  • Mã hóa và giải mã các tệp PDF bằng mật khẩu and decrypt PDF files with passwords

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

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
46. Bạn đã học được cách:You learned how to:

  • Sử dụng lớp
    >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    52
  • Viết văn bản vào
    >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    52 bằng
    >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    62
    text to a
    >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    52 with
    >>> expense_reports = list[reports_dir.glob["*.pdf"]]
    >>> expense_reports.sort[]
    
    62
  • Đặt kích thước phông chữ và phông chữ với
    >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    12font and font size with
    >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    12
  • Thay đổi màu phông chữ bằng
    >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    34font color with
    >>> for path in expense_reports:
    ...     print[path.name]
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    34

>>> expense_reports = list[reports_dir.glob["*.pdf"]]
>>> expense_reports.sort[]
46 là một công cụ tạo PDF mạnh mẽ và bạn chỉ làm trầy xước bề mặt của những gì có thể. Nếu bạn thích những gì bạn đã học trong mẫu này từ Python Basics: Giới thiệu thực tế về Python 3, thì hãy chắc chắn kiểm tra phần còn lại của cuốn sách.

Mã hóa hạnh phúc!

Bài Viết Liên Quan

Chủ Đề