Hướng dẫn how do i edit a pdf file in python? - làm cách nào để chỉnh sửa tệp 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.

Show

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.

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

  • Đọc văn bản từ PDF text from a PDF
  • Chia PDF thành nhiều tệp a PDF into multiple files
  • Concatenate và hợp nhất các tệp PDF and merge PDF files
  • Xoay và cắt các trang trong tệp PDF and crop pages in a PDF file
  • Mã hóa và giải mã các tệp PDF bằng mật khẩu and decrypt PDF files with passwords
  • Tạo tệp PDF từ đầu a PDF file from scratch

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:

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

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.

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

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

Sử dụ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

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

Sự khác biệt chính giữa hai là

>>> 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"
6 chỉ có thể nối hoặc nối, các trang vào cuối danh sách các trang đã có trong người viết, trong khi
>>> 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ó thể chèn hoặc hợp nhất các trang tại bất kỳ vị trí nào.

Hãy tiếp tục và 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 đầu tiên của bạn. Trong cửa sổ tương tác Idle, hãy nhập mã sau để 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:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger()

Các đố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 trống khi chúng khởi tạo lần đầu tiên. Bạn cần phải thêm một số trang vào đối tượng của mình trước khi bạn có thể làm bất cứ điều gì với nó.

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

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
2 và cách nào bạn sử dụng phụ thuộc vào những gì bạn cần thực hiện:

  • >>> pdf_merger = PdfFileMerger()
    >>> pdf_merger.append(str(report_path))
    
    3 liên kết mọi trang trong tài liệu PDF hiện có đến cuối các trang hiện tại 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"
    
    3.
    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.
  • >>> pdf_merger = PdfFileMerger()
    >>> pdf_merger.append(str(report_path))
    
    5 chèn tất cả các trang trong tài liệu PDF hiện có sau một trang cụ thể 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"
    
    3.
    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.

Bạn sẽ xem xét cả hai phương thức trong phần này, bắt đầu bằng

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
3.

Ghép các tệp PDF với >>> pdf_merger = PdfFileMerger() >>> pdf_merger.append(str(report_path)) 3

Thư mục

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
9 có một thư mục con gọi là
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
00 có chứa ba báo cáo chi phí cho một nhân viên tên là Peter Python.

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

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

>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
01 để có được danh sách các đối tượng
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
02 cho mỗi trong ba báo cáo chi phí trong thư mục
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
03:

>>>

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

Các đố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 trống khi chúng khởi tạo lần đầu tiên. Bạn cần phải thêm một số trang vào đối tượng của mình trước khi bạn có thể làm bất cứ điều gì với nó.

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

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
2 và cách nào bạn sử dụng phụ thuộc vào những gì bạn cần thực hiện:

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
3 liên kết mọi trang trong tài liệu PDF hiện có đến cuối các trang hiện tại 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"
3.

>>>

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

Các đố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 trống khi chúng khởi tạo lần đầu tiên. Bạn cần phải thêm một số trang vào đối tượng của mình trước khi bạn có thể làm bất cứ điều gì với nó.

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

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
2 và cách nào bạn sử dụng phụ thuộc vào những gì bạn cần thực hiện:

>>>

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()

Các đố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 trống khi chúng khởi tạo lần đầu tiên. Bạn cần phải thêm một số trang vào đối tượng của mình trước khi bạn có thể làm bất cứ điều gì với nó.

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

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
2 và cách nào bạn sử dụng phụ thuộc vào những gì bạn cần thực hiện:

>>>

>>> for path in expense_reports:
...     print(path.name)
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf

Trông tôt đây!

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"
... )
15, 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))
3, tất cả các trang trong tệp PDF đều được thêm 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:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger()

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

>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
00 được sắp xếp và nối chúng vào
>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
2:

>>>

>>> for path in expense_reports:
...     pdf_merger.append(str(path))
...

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"
... )
02 trong
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
03 đượ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"
... )
23 trước khi được chuyển sang
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
24.

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"
... )
03 được nối với nhau trong đối tượng
>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
2, đ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"
... )
28 hoạt động giống như
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
29.

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"
... )
30:

>>>

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

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"
... )
31. 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.

Hợp nhất PDFS với >>> pdf_merger = PdfFileMerger() >>> pdf_merger.append(str(report_path)) 5

Để 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"
... )
33. Phương pháp này tương tự như
>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
3, 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.

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"
... )
35 của thư mục
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
36. 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"
... )
37 và mục nội dung nằm trong một tệp có tên là
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
38.

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 ____102 cho các tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
37 và
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
38:

>>>

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

Đ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))
3:

>>>

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))

Bây giờ

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
2 có một số trang trong đó, bạn có thể hợp nhất bảng nội dung PDF vào đó 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"
... )
37 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"
... )
47 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"
... )
47 và trước trang tại Index
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
50.

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

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

  1. Số nguyên
    >>> from pathlib import Path
    >>> reports_dir = (
    ...     Path.home()
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... )
    
    50, chỉ ra chỉ mục của trang mà bảng nội dung nên được chèn
  2. 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ư:

>>>

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

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"
... )
50. 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"
... )
50. Trang hiện đang ở Index
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
50 sau đó được chuyển sang Index
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
56. Trang hiện đang ở Index
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
56 được chuyển sang Index
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
58, v.v.

Bây giờ hãy viết PDF được hợp nhất vào tệp đầu ra:

>>>

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

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

>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
59 trong thư mục làm việc hiện tại của bạn. Mở nó với đầu đọc PDF và kiểm tra xem bảng nội dung đã được chèn vào đúng vị trí.

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

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

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

Trong thư mục

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
9 trong kho lưu trữ đồng hành cho bài viết này, có hai tệp được gọi là
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
61 và
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
62.

Sử dụng một thể hiện

>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
63, kết hợp hai tệp bằng cách sử dụng
>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
3. 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"
... )
65 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"
... )
67 và
>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
3 mỗi tệp thành
>>> 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"
... )
4

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

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
2 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"
... )
65 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"
... )
73 trong thư mục
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
36. Tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
73 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"
... )
76 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"
6 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"
... )
02 từ mô -đun
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
01:

>>>

>>> 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"
... )
02 cho tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
73:

>>>

>>> 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"
... )
02 cho tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
73:

>>>

>>> 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"
... )
02 cho tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
73:

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"
... )
76 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"
6 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"
... )
85 để 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"
... )
86. 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"
... )
87 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"
... )
88, 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.

>>>

>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
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"
... )
02 cho tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
73:

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"
... )
76 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"
6 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"
... )
85 để 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 độ.

>>>

>>> 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"
... )
02 cho tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
73:

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"
... )
76 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"
6 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"
... )
85 để 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"
... )
86. 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"
... )
87 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"
... )
02 cho tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
73:

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"
... )
76 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"
6 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"
... )
02 cho tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
73:

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"
... )
76 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"
6 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"
... )
02 cho tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
73:

>>>

>>> 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"
... )
02 cho tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
73:

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"
... )
76 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"
6 mới:

>>>

>>> 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ờ tạo một đối tượng

>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
02 cho tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
73:

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"
... )
76 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"
6 mới:

>>>

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

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
20 có thể điều chỉnh được, kiểm tra giá trị của
>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
00 và xoay trang nếu giá trị đó là
>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
01:

>>>

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

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"
... )
85 vào một tệp 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 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
24 trong thư mục làm việc hiện tại của bạn và so sánh nó với tệp
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
94 bạn đã tạo trước đó. Họ nên trông giống hệt nhau.

Giá trị của

>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
00 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
>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
00 có thể có giá trị
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
47.

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

Trang cắt xén

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.

Ví dụ: thư mục

>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
36 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
30. 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.

Để 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"
... )
76 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"
6 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"
... )
02 từ mô -đun
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
01:

>>>

>>> 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"
... )
02 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
30:

>>>

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
0

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"
... )
76 mới và nhận trang đầu tiên của PDF:

>>>

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
1

Để 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"
... )
99 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
40 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
41 đạ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
41 trước khi sử dụng nó cắt trang:

>>>

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
2

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
41 trả về
>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
44. Đố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.

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

>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
47 đạ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
48 đ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
49.

A

>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
44 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
51,
>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
52,
>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
53 và
>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
54. 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 để 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
44:

>>>

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
3

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

>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
56 chứa tọa độ của góc được chỉ định. Bạn có thể truy cập các tọa độ riêng lẻ bằng dấu ngoặc vuông giống như bất kỳ Python Tuple nào khác:

>>>

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
4

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

>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
57 bằng cách gán một bộ mới cho một trong các thuộc tính của nó:

>>>

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
5

Khi bạn 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
53, 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
54 sẽ tự động điều chỉnh để bảo tồn hình chữ nhật:

>>>

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
6

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

>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
44 được trả về bởi
>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
41, bạn sẽ cắt trang một cách hiệu quả. Đố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
40 hiện chỉ chứa thông tin có trong ranh giớ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
44 mới.

Đi trước và viết trang bị cắt vào tệp PDF mới:

>>>

>>> 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
64 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
54 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
41. 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"
... )
76 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"
6 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
06 và thêm nó vào
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
85:

>>>

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

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

>>>

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

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

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
71 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
72 để tạo một bản sao của trang:

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
73 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
40. 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
40 sau để trích xuất văn bản ở phía bên phải của trang.

>>>

>>> for path in expense_reports:
...     print(path.name)
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
1

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

>>>

>>> for path in expense_reports:
...     print(path.name)
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
2

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

>>>

>>> for path in expense_reports:
...     print(path.name)
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
3

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
71 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
72 để tạo một bản sao của trang:

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
73 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
40. 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
40 sau để trích xuất văn bản ở phía bên phải của trang.

>>>

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

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
41 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
56 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
54.

>>>

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

Đầ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
41.

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
56 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
6

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
54:

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
40:

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
53 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
54:

Đ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
85 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 trang

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

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

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

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

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

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:
...     print(path.name)
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
9

Trong thư mục

>>> pdf_merger = PdfFileMerger()
>>> pdf_merger.append(str(report_path))
9 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
91.

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger()
0

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 92 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 91, 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:

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"
... )
76 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"
6:

  1. 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
    
    06, 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
    
    97 và thêm chúng vào
    >>> from pathlib import Path
    >>> reports_dir = (
    ...     Path.home()
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... )
    
    85:
    sets the user password. This allows for opening and reading the PDF file.
  2. 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"
    ... )
    
    85 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
    
    92 trong thư mục nhà máy tính của bạn:
    sets the owner password. This allows for opening the PDF without any restrictions, including editing.

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

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger()
1

Đô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ó.

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger()
2

Mã hóa PDFS

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger()
3

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

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
02 của phiên bản
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
03. Nó có hai tham số chính:

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
04 đặt mật khẩu người dùng. Điều này cho phép mở và đọc tệp PDF.

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger()
4

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
05 đặt mật khẩu 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.

Hãy để sử dụng

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
02 để 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()
07 trong thư mục
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
36:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger()
5

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"
6 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
06 vào nó:

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

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
11 với
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
12:

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()
21 của một thể hiện
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
76.

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
21 có một tham số duy nhất gọi là
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
24 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()
24.

Hãy để mở tệp

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
15 đượ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"
... )
76 mới với đường dẫn đến PDF được bảo vệ:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger()
6

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:

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger()
7

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()
29 được nêu ra, thông báo cho bạn rằng tệp PDF chưa được giải mã.

>>>

>>> from PyPDF2 import PdfFileMerger
>>> pdf_merger = PdfFileMerger()
8

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()
    
    29 được nêu ra, thông báo cho bạn rằng tệp PDF chưa được giải mã.
    indicates that the password is incorrect.
  • Đi trước và giải mã tập tin ngay bây giờ: indicates that the user password was matched.
  • >>> expense_reports = list(reports_dir.glob("*.pdf"))
    >>> expense_reports.sort()
    
    21 Trả về một số nguyên đại diện cho sự thành công của giải mã:
    indicates that the owner password was matched.

>>> 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 không chính xác.

>>>

>>> 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() 29 đượ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()
21 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"
... )
47 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"
... )
50 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"
... )
56 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)) 9 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() 35.

Sử dụng

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
36, 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()
37. Lưu tệp được mã hóa dưới dạng
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
38 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"
... )
76 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"
6:

>>> 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
06 lên
>>> from pathlib import Path
>>> reports_dir = (
...     Path.home()
...     / "creating-and-modifying-pdfs"
...     / "practice_files"
...     / "expense_reports"
... )
85 bằng cách sử dụng
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
43:

>>> 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()
44 để đặt người dùng Passwrod thành
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
45:

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" ... ) 85 vào một tệp có tên >>> expense_reports = list(reports_dir.glob("*.pdf")) >>> expense_reports.sort() 38 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

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:

>>>

>>> for path in expense_reports:
...     pdf_merger.append(str(path))
...
8

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()
29 được nêu ra, thông báo cho bạn rằng tệp PDF chưa được giải mã.

>>>

>>> for path in expense_reports:
...     pdf_merger.append(str(path))
...
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()
29 đượ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()
21 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"
... )
47 chỉ ra rằng mật khẩu không chính xác.

  1. >>> from pathlib import Path
    >>> reports_dir = (
    ...     Path.home()
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... )
    
    50 chỉ ra rằng mật khẩu người dùng được khớp.
  2. >>> from pathlib import Path
    >>> reports_dir = (
    ...     Path.home()
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... )
    
    56 chỉ ra rằng mật khẩu của chủ sở hữu đã được khớp.

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

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!

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

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

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
77 inch bằng
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
78 inch, bạn sẽ tạo ra
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
55 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()
80 đại diện cho một bài báo có kích thước chữ cái vì
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
77 lần
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
82 là
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
83 và
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
78 lần
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
82 là
>>> for path in reports_dir.glob("*.pdf"):
...     print(path.name)
...
Expense report 1.pdf
Expense report 3.pdf
Expense report 2.pdf
48.

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()
87 để giúp bạn chuyển đổi. Mô -đun
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
88 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()
89 và
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
90, đơ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()
89 và
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
90 từ mô -đun
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
87:

>>>

>>> 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()
90 và
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
89 đề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()
89 là
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
97 điểm và
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
90 là
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
99 đ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()
90 và
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
89 đề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()
89 là
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
97 điểm và
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
90 là
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
99 đ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()
90 và
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
89 đề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()
89 là
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
97 điểm và
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
90 là
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
99 đ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()
89 để đặt kích thước trang thành rộng ____377 inch bằng
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
78 inch cao:
Bằng cách chuyển một tuple cho
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
76, 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()
49 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
05. 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
06 từ mô -đun
>>> for path in expense_reports:
...     print(path.name)
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
07 và chuyển nó vào tham số
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
76 khi khởi tạo
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
55 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
06, 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
11 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
12
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
06

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
14

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()
90 và
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
89 đề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()
89 là
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
97 điểm và
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
90 là
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
99 điểm.

  1. >>> for path in expense_reports:
    ...     print(path.name)
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    23
  2. >>> for path in expense_reports:
    ...     print(path.name)
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    24
  3. >>> for path in expense_reports:
    ...     print(path.name)
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    25

Để 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()
89 để đặt kích thước trang thành rộng ____377 inch bằng
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
78 inch cao:

  • >>> 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
    
    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
    
    24
  • >>> 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
    
    33
  • >>> for path in expense_reports:
    ...     print(path.name)
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    34
  • >>> for path in expense_reports:
    ...     print(path.name)
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    35
  • >>> for path in expense_reports:
    ...     print(path.name)
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    36
  • >>> for path in expense_reports:
    ...     print(path.name)
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    37
  • >>> for path in expense_reports:
    ...     print(path.name)
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    25

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

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
76, 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()
49 có một số kích thước trang tích hợp tiêu chuẩn dễ làm việc hơn.

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

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
05. 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
06 từ mô -đun
>>> for path in expense_reports:
...     print(path.name)
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
07 và chuyển nó vào tham số
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
76 khi khởi tạo
>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
55 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
06, 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
11 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
12

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
06

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

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
14

8,5 trong x 14 in

>>> for path in expense_reports:
...     print(path.name)
...
Expense report 1.pdf
Expense report 2.pdf
Expense report 3.pdf
15

11 trong x 17 in

Ngoài ra, mô -đun chứa các định nghĩa cho tất cả các kích thước giấy tiêu chuẩn ISO 216.

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"
    ... )
    
    76
    PDF files and extract text using the
    >>> from pathlib import Path
    >>> reports_dir = (
    ...     Path.home()
    ...     / "creating-and-modifying-pdfs"
    ...     / "practice_files"
    ...     / "expense_reports"
    ... )
    
    76 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"
    
    6
    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"
    
    6 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()
49. 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()
    
    55
  • Viết văn bản cho
    >>> expense_reports = list(reports_dir.glob("*.pdf"))
    >>> expense_reports.sort()
    
    55 với
    >>> expense_reports = list(reports_dir.glob("*.pdf"))
    >>> expense_reports.sort()
    
    67
    text to a
    >>> expense_reports = list(reports_dir.glob("*.pdf"))
    >>> expense_reports.sort()
    
    55 with
    >>> expense_reports = list(reports_dir.glob("*.pdf"))
    >>> expense_reports.sort()
    
    67
  • Đặ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
    
    17font and font size with
    >>> for path in expense_reports:
    ...     print(path.name)
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    17
  • 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
    
    39font color with
    >>> for path in expense_reports:
    ...     print(path.name)
    ...
    Expense report 1.pdf
    Expense report 2.pdf
    Expense report 3.pdf
    
    39

>>> expense_reports = list(reports_dir.glob("*.pdf"))
>>> expense_reports.sort()
49 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!

Python có thể hoạt động với pdfs không?

Bạn có thể làm việc với PDF có sẵn trong Python bằng cách sử dụng gói PYPDF2. PYPDF2 là gói python thuần túy mà bạn có thể sử dụng cho nhiều loại hoạt động PDF khác nhau.. PyPDF2 is a pure-Python package that you can use for many different types of PDF operations.

Chúng ta có thể chỉnh sửa dữ liệu trong PDF không?

Định dạng tài liệu di động (PDF) là một định dạng phổ biến để chia sẻ các phiên bản cuối cùng của các tệp.Để thêm hoặc chỉnh sửa văn bản trong PDF được thực hiện trong một chương trình văn phòng như Excel hoặc Nhà xuất bản, hãy bắt đầu với tệp văn phòng gốc.Mở tệp đó trong chương trình Office của bạn, thực hiện các thay đổi của bạn và sau đó lưu lại tệp ở định dạng PDF.