Trích xuất Trang từ PDF trong Python SDK
Trích xuất Trang từ Tài liệu PDF sử dụng Cloud sử dụng Cloud Python SDK.
Get StartedCách để Lấy Trang từ PDF qua Cloud Python SDK
Để trích xuất Trang từ PDF, chúng ta sẽ sử dụng Aspose.PDF Cloud Python SDK. Cloud SDK này giúp các lập trình viên Python phát triển các ứng dụng tạo, đánh dấu, chỉnh sửa và chuyển đổi tài liệu PDF dựa trên nặng-based dưới dạng hai câu Aspose.PDF REST API. Chỉ cần tạo một tài khoản tại Aspose for Cloud và lấy thông tin ứng dụng của bạn. Khi bạn có App SID & key, bạn đã sẵn sàng sử dụng Aspose.PDF Cloud Python SDK. Nếu gói python được đăng[]> tải trên Github, bạn có thể cài đặt trực tiếp từ Github:
Cài đặt từ Github
pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git
Lệnh của Package Manager Console
pip install asposepdfcloud
Bước để Lấy Trang từ PDF qua Python SDK
Các nhà phát triển Aspose.PDF Cloud có thể dễ dàng tải & trích xuất trang từ PDF chỉ trong vài dòng mã.
- Cài đặt Python SDK
- Tải lên một tài liệu PDF lên máy chủ Aspose Cloud
- Tải xuống tài liệu PDF đã xử lý từ máy chủ Aspose Cloud
- Lấy thông tin trang của tài liệu PDF
Trích xuất Trang từ PDF bằng Python
import shutil
import json
import logging
from pathlib import Path
from asposepdfcloud import ApiClient, PdfApi, DocumentPagesResponse
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
class Config:
"""Configuration parameters."""
CREDENTIALS_FILE = Path(r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json")
LOCAL_FOLDER = Path(r"C:\Samples")
PDF_DOCUMENT_NAME = "sample.pdf"
LOCAL_RESULT_DOCUMENT_NAME = "output_sample.png"
PAGE_NUMBER = 2
class PdfPages:
""" Class for managing PDF pages using Aspose PDF Cloud API. """
def __init__(self, credentials_file: Path = Config.CREDENTIALS_FILE):
self.pdf_api = None
self._init_api(credentials_file)
def _init_api(self, credentials_file: Path):
""" Initialize the API client. """
try:
with credentials_file.open("r", encoding="utf-8") as file:
credentials = json.load(file)
api_key, app_id = credentials.get("key"), credentials.get("id")
if not api_key or not app_id:
raise ValueError("init_api(): Error: Missing API keys in the credentials file.")
self.pdf_api = PdfApi(ApiClient(api_key, app_id))
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
logging.error(f"init_api(): Failed to load credentials: {e}")
def upload_document(self):
""" Upload a PDF document to the Aspose Cloud server. """
if self.pdf_api:
file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME
try:
self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path))
logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.")
except Exception as e:
logging.error(f"upload_document(): Failed to upload file: {e}")
def get_page_info(self):
""" Get page information of the PDF document. """
if self.pdf_api:
result_pages: DocumentPagesResponse = self.pdf_api.get_page(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
if result_pages.code == 200:
logging.info(f"Page #{Config.PAGE_NUMBER} information: {result_pages.page}")
else:
logging.error(f"Failed to get the page #{Config.PAGE_NUMBER}.")
def get_page_as_png(self):
""" Get page information of the PDF document. """
if self.pdf_api:
try:
result_pages = self.pdf_api.get_page_convert_to_png(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
shutil.move(result_pages, str(local_path))
logging.info(f"download_result(): File successfully downloaded: {local_path}")
except Exception as e:
logging.error(f"download_result(): Failed to download file: {e}")
if __name__ == "__main__":
pdf_pages = PdfPages()
pdf_pages.upload_document()
pdf_pages.get_page_info()
pdf_pages.get_page_as_png()
Làm việc với Trang trong PDF
Trích xuất trang từ PDF là một tác vụ thường gặp phục vụ nhiều mục đích khác nhau trong nhiều bối cảnh. Quá trình này liên quan đến việc chọn các trang cụ thể từ một tài liệu lớn hơn để tạo một tệp PDF mới, riêng biệt. Hiểu lý do Behind this practice có thể giúp việc quản lý và sử dụngdocument. quản lý và sử dụng PDF Lyn.Theof à zooof àeof;\n\Greater in which context toclassify ehိမ}, hindrance or invarian.
Large PDF files can be cumbersome to share or store. F anyự abandonment).scale incomplete.includegraphics[height=1cm]g statutory the authoring substantial limitations.PDF\ .\7of iumin_ _fond.088 essi creed.PDF\ .\nthreaten of iumen- of.ger maabyyxبلغ التشر لو م create smaller, more manageable files. This is particularly useful when only a portion of the document is relevant for a specific purpose. For instance, removing unnecessary pages can significantly decrease the file size, making it easier to handle and distribute. Trích xuất trang cho phép người dùng tái sử dụng nội dung cho các ứng dụng khác. Ví dụ, một người có thể trích xuất trang từ một báo cáo toàn diện để tạo một tóm tắt độc lập hoặc để cô lập dữ liệu nhất định cho phân tích. Điều này cho phép tái sử dụng nội dung hiện có mà không cần phải tạo lại thông tin từ đầu. Trích xuất trang từ tài liệu PDF với Aspose.PDF Cloud Python SDK.
Với thư viện Python của chúng tôi bạn có thể:
- Kết hợp tài liệu PDF.
- Chia các tệp PDF.
- Chuyển đổi PDF sang các định dạng khác và ngược lại.
- Thao tác với các chú thích.
- Làm việc với Hình ảnh trong PDF, v.v.
- Bạn có thể thử ứng dụng miễn phí của chúng tôi để thử tính năng trực tuyến.