Python(Java 経由)で PPT と PPTX を PDF に変換(高度な機能を含む)

概要

Python(Java 経由)で PowerPoint プレゼンテーション(PPT、PPTX、ODP など)を PDF 形式に変換すると、さまざまなデバイス間での互換性やプレゼンテーションのレイアウトと書式設定の保持など、複数の利点があります。このガイドでは、プレゼンテーションを PDF ドキュメントに変換する方法、画像品質を制御する各種オプションの使用方法、非表示スライドの含め方、PDF ファイルへのパスワード保護、フォント置換の検出、変換対象スライドの選択、そして出力ドキュメントへのコンプライアンス標準の適用方法を示します。

PowerPoint から PDF への変換

Aspose.Slides を使用すると、次の形式のプレゼンテーションを PDF に変換できます:

  • PPT
  • PPTX
  • ODP

プレゼンテーションを PDF に変換するには、ファイル名を引数として Presentation クラスに渡し、save メソッドを使用して PDF として保存します。Presentation クラスは、プレゼンテーションを PDF に変換する際に一般的に使用される save メソッドを提供します。

Aspose.Slides では、次の変換が可能です:

  • プレゼンテーション全体を PDF に変換
  • プレゼンテーションから特定のスライドを PDF に変換

Aspose.Slides はプレゼンテーションを PDF にエクスポートし、生成された PDF が元のプレゼンテーションとほぼ同一になるよう保証します。変換時には、以下を含む要素と属性が正確にレンダリングされます:

  • 画像
  • テキスト ボックスとシェイプ
  • テキスト書式設定
  • 段落書式設定
  • ハイパーリンク
  • ヘッダーとフッター
  • 箇条書き

PowerPoint を PDF に変換

標準の変換はデフォルトの PDF エクスポート設定を使用します。画像品質、ページ内容、または PDF のコンプライアンスを制御したい場合は、カスタムオプションを使用してください。

サンプルを実行する前に、Aspose.Slides for Python via Java と互換性のある Java ランタイムをインストールしてください。各サンプルはカレントディレクトリから presentation.pptx を読み取りますので、対象の PPT、PPTX、または ODP ファイルに置き換えてください。JVM は Python プロセスごとに一度だけ起動します。

このコードはプレゼンテーションを PDF に変換します:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat

presentation = Presentation("presentation.pptx")
try:
    presentation.save("presentation.pdf", SaveFormat.Pdf)
finally:
    presentation.dispose()

オプション付きで PowerPoint を PDF に変換

Aspose.Slides は、PdfOptions クラスのプロパティとしてカスタムオプションを提供し、生成される PDF のカスタマイズ、パスワードによるロック、変換プロセスの進行方法を指定できます。

カスタムオプションで PowerPoint を PDF に変換

カスタム変換オプションを使用すると、ラスター画像の品質設定、メタファイルの取り扱い方法、テキストの圧縮レベル、画像の DPI 設定など、さまざまな項目を指定できます。

以下のコード例は、複数のカスタムオプションを使用して PowerPoint プレゼンテーションを PDF に変換する方法を示しています。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import PdfCompliance, PdfOptions, PdfTextCompression, Presentation, SaveFormat

presentation = Presentation("presentation.pptx")
try:
    pdf_options = PdfOptions()
    pdf_options.setJpegQuality(jpype.JByte(90))
    pdf_options.setSufficientResolution(300)
    pdf_options.setSaveMetafilesAsPng(True)
    pdf_options.setTextCompression(PdfTextCompression.Flate)
    pdf_options.setCompliance(PdfCompliance.Pdf15)
    presentation.save("presentation-custom.pdf", SaveFormat.Pdf, pdf_options)
finally:
    presentation.dispose()

非表示スライドを含めて PowerPoint を PDF に変換

プレゼンテーションに非表示スライドが含まれている場合、PdfOptions クラスの setShowHiddenSlides メソッドを使用して、非表示スライドを生成される PDF のページとして含めることができます。

以下のコードは、非表示スライドを含めて PowerPoint プレゼンテーションを PDF に変換する方法を示しています。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import PdfOptions, Presentation, SaveFormat

presentation = Presentation("presentation.pptx")
try:
    pdf_options = PdfOptions()
    pdf_options.setShowHiddenSlides(True)
    presentation.save("presentation-hidden-slides.pdf", SaveFormat.Pdf, pdf_options)
finally:
    presentation.dispose()

パスワード保護された PDF に PowerPoint を変換

このコードは、PdfOptions クラスの保護パラメータを使用して、PowerPoint プレゼンテーションをパスワード保護された PDF に変換する方法を示しています。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import PdfAccessPermissions, PdfOptions, Presentation, SaveFormat

presentation = Presentation("presentation.pptx")
try:
    pdf_options = PdfOptions()
    pdf_options.setPassword("password")
    permissions = PdfAccessPermissions.PrintDocument | PdfAccessPermissions.HighQualityPrint
    pdf_options.setAccessPermissions(permissions)
    presentation.save("presentation-protected.pdf", SaveFormat.Pdf, pdf_options)
finally:
    presentation.dispose()

フォント置換の検出

Aspose.Slides は、PdfOptions クラスの下にある setWarningCallback メソッドを提供し、プレゼンテーションから PDF への変換プロセス中にフォント置換を検出できるようにします。

JPype プロキシを使用して Java API からの警告コールバックを受け取り、Java の説明文字列を Python の文字列に変換してプレフィックスを確認します:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import PdfOptions, Presentation, ReturnAction, SaveFormat, WarningType

class FontSubstitutionHandler:
    def warning(self, warning):
        description = str(warning.getDescription())
        if warning.getWarningType() == WarningType.DataLoss and description.startswith("Font will be substituted"):
            print(f"Font substitution warning: {description}")
        return ReturnAction.Continue


presentation = Presentation("presentation.pptx")
try:
    handler = FontSubstitutionHandler()
    callback = jpype.JProxy("com.aspose.slides.IWarningCallback", inst=handler)
    pdf_options = PdfOptions()
    pdf_options.setWarningCallback(callback)
    presentation.save("presentation-font-warnings.pdf", SaveFormat.Pdf, pdf_options)
finally:
    presentation.dispose()

PowerPoint の選択スライドを PDF に変換

Presentation.save に渡すスライド番号は 1 から始まります。この例では、スライド 1 と 3 が存在する場合にそれらをエクスポートします:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat

presentation = Presentation("presentation.pptx")
try:
    if presentation.getSlides().size() >= 3:
        slide_numbers = jpype.JArray(jpype.JInt)([1, 3])
        presentation.save("presentation-selected-slides.pdf", slide_numbers, SaveFormat.Pdf)
    else:
        print("The presentation must contain at least three slides.")
finally:
    presentation.dispose()

カスタムスライドサイズで PowerPoint を PDF に変換

この例は、サイズ 612 x 792 ポイント(US Letter)のページ上に最初のスライドをエクスポートします。指定したサイズの新しいプレゼンテーションにスライドをクローンします:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat, SlideSizeScaleType

presentation = Presentation("presentation.pptx")
try:
    resized_presentation = Presentation()
    try:
        resized_presentation.getSlideSize().setSize(612.0, 792.0, SlideSizeScaleType.EnsureFit)
        if presentation.getSlides().size() > 0:
            slide = presentation.getSlides().get_Item(0)
            resized_presentation.getSlides().insertClone(0, slide)
            resized_presentation.getSlides().removeAt(1)
            resized_presentation.save("presentation-custom-size.pdf", SaveFormat.Pdf)
        else:
            print("The presentation contains no slides.")
    finally:
        resized_presentation.dispose()
finally:
    presentation.dispose()

ノートスライドビューで PowerPoint を PDF に変換

このコードは、ノートを含む PDF に PowerPoint プレゼンテーションを変換する方法を示しています:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import NotesCommentsLayoutingOptions, NotesPositions, PdfOptions, Presentation, SaveFormat

presentation = Presentation("presentation.pptx")
try:
    notes_options = NotesCommentsLayoutingOptions()
    notes_options.setNotesPosition(NotesPositions.BottomFull)
    pdf_options = PdfOptions()
    pdf_options.setSlidesLayoutOptions(notes_options)
    presentation.save("presentation-with-notes.pdf", SaveFormat.Pdf, pdf_options)
finally:
    presentation.dispose()

PDF のアクセシビリティとコンプライアンス標準

アクセシブルな PDF を作成する際は、Web Content Accessibility Guidelines (WCAG) を参照してください。出力標準として PDF/A1aPDF/A1bPDF/UA を選択するには、PdfOptions.setCompliance を使用します。

以下のコードは、異なるコンプライアンス標準に基づいて�数の PDF を生成する PowerPoint から PDF への変換プロセスを示しています:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import PdfCompliance, PdfOptions, Presentation, SaveFormat

presentation = Presentation("presentation.pptx")
try:
    pdf_options = PdfOptions()
    pdf_options.setCompliance(PdfCompliance.PdfA1a)
    presentation.save("presentation-a1a.pdf", SaveFormat.Pdf, pdf_options)
    pdf_options.setCompliance(PdfCompliance.PdfA1b)
    presentation.save("presentation-a1b.pdf", SaveFormat.Pdf, pdf_options)
    pdf_options.setCompliance(PdfCompliance.PdfUa)
    presentation.save("presentation-ua.pdf", SaveFormat.Pdf, pdf_options)
finally:
    presentation.dispose()

注意: PDF/UA にエクスポートする場合、Aspose.Slides は SmartArt、チャート、数式などの複雑なグラフィックを単一の図として扱います。個々のパス要素は別個のコンテンツとして保持されず、アーティファクトとしてマークされることがあります。代替テキストは全体の図に対してのみ提供されます。

FAQ

複数の PowerPoint ファイルを一括で PDF に変換できますか?
はい、Aspose.Slides は複数の PPT または PPTX ファイルを一括で PDF に変換するバッチ変換をサポートしています。ファイルを列挙し、プログラムから変換プロセスを適用できます。

変換された PDF にパスワード保護を設定できますか?
はい。変換プロセス中に PdfOptions クラスを使用してパスワードを設定し、アクセス許可を定義できます。

PDF に非表示スライドを含めるにはどうすればよいですか?
PDF に非表示スライドを含めるには、PdfOptions クラスの setShowHiddenSlides メソッドを使用します。

Aspose.Slides は PDF の画像品質を高く保つことができますか?
はい、setJpegQualitysetSufficientResolution などのメソッドを PdfOptions クラスで使用して、PDF の画像品質を高く保つことができます。

Aspose.Slides は PDF/A コンプライアンス標準をサポートしていますか?
はい、Aspose.Slides は、PDF/A1a、PDF/A1b、PDF/UA などの さまざまな標準 に準拠した PDF のエクスポートを可能にします。目的に応じて適切な標準を選択し、出力が要件を満たしているか確認してください。

追加リソース