PNG JPG BMP TIFF PDF
Aspose.PDF  for Python

Python SDKでPDFをクロップ

Cloud Python SDKを使用してPDFドキュメントをクロップします。

Get Started

Cloud Python SDKを使用してPDFをクロップする方法

PDFをクロップするには、 Aspose.PDF Cloud Python SDKを使用します。このCloud SDKは、Aspose.PDF REST APIを介してPythonプログラミング言語を使用してクラウドベースのPDF作成、注釈、編集、変換アプリを開発するPythonプログラマを支援します。PythonパッケージがGithubにホストされている場合、直接Githubからインストールできます。

Githubからのインストール


     
    pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git

パッケージマネージャコンソールコマンド

     
    pip install asposepdfcloud

Python SDKを使用してPDFをクロップする手順

Aspose.PDF Cloud開発者は、わずか数行のコードでPDFを簡単に読み込み・クロップできます。

  1. Python SDKをインストール
  2. オリジナルファイルをAspose Cloudにアップロード
  3. ページメタデータを取得(オプション、内部で使用される可能性あり)
  4. 希望のクロップ寸法でページを画像として抽出
  5. 指定された幅と高さで新しいPDFを作成
  6. クロップされたページ(画像)を新しいドキュメントに挿入
  7. ドキュメントをダウンロードしてローカルに保存
 

Pythonを使用してPDFをクロップ


    import os
    import logging
    from pathlib import Path
    from asposepdfcloud import PdfApi, AsposeResponse, DocumentResponse
    from change_layout_helper import PdfChangeLayoutHelper, Config

    class PdfCropPages:
        def __init__(self, pdf_api: PdfApi, helper: PdfChangeLayoutHelper):
            self.pdfApi = pdf_api
            self.helper = helper

        def cropDocumentPage(self, documentName: str, pageNumber: int, llx: int, lly: int, width: int, height: int, outputDocument: str, localFolder: str, tempFolder: str):
            self.helper.uploadFile(documentName, localFolder, tempFolder)

            self.helper.getPageInfo(documentName, pageNumber, tempFolder)

            imageFile = self.helper.extractPdfPage(documentName, pageNumber, Config.CROP_PAGE_WIDTH, Config.CROP_PAGE_HEIGHT, localFolder, tempFolder)
            newPdf: DocumentResponse = self.helper.createDocument(outputDocument, width, height, tempFolder)
            if newPdf.code != 200:
                logging.error("cropDocumentPage(): Failed to create new PDF document!")
            else:
                response: AsposeResponse = self.helper.insertPageAsImage(outputDocument, imageFile, llx, lly, tempFolder)
                if response.code != 200:
                    logging.error("cropDocumentPage(): Can't crop pdf document page!")
                else:
                    logging.info("cropDocumentPage(): Page successfully cropped.")
                    self.helper.downloadFile(outputDocument, outputDocument, localFolder, tempFolder, "cropped_")
 

私たちのPythonライブラリを使用してできること:

Aspose.PDF Cloud Python SDKを使用してPDFドキュメントをクロップ。

  • PDFドキュメントを結合。
  • PDFファイルを分割。
  • PDFを他の形式に変換、またその逆。
  • 注釈を操作。
  • PDF内の画像を操作、など。
  • 無料アプリを試して、オンラインで機能をテストできます。