Create a PDF document using Python

PDF ファイル形式は、ドキュメントの提示と交換に一般的に使用されます。さらに、レイアウトやフォーマットを失うことなく、MB を削減し、テキスト、画像、表、注釈、ブックマーク、ハイパーリンクをレンダリングするなど、多くの利点があります。 Python 開発者は、これらの利点を考慮して、データを PDF として公開することを検討する場合があります。あなたがそのような開発者の 1 人である場合、この記事は、Python を使用して PDF ドキュメントを作成する方法を学ぶのに役立ちます。

この記事では、次の点について説明します。

Python PDF Creator パッケージのインストール

プログラムで PDF ドキュメントを作成するには、Python PDF クリエーター パッケージを使用します。これにより、開発者は Adobe Acrobat を使用せずに PDF ファイルを生成、読み取り、変換、および操作できます。

ダウンロードするか、以下に示す pip コマンドを使用して PyPI からパッケージをインストールしてください。

pip install aspose-pdf

Python で PDF ドキュメントを作成する

まず、テキストフラグメントを含む PDF ドキュメントを作成する方法を説明します。以下は、単純な PDF ドキュメントをゼロから作成する手順です。

  • Document クラスのインスタンスを作成します。
  • 新しい Page をドキュメントのページ コレクションに追加します。
  • 新しい TextFragment を作成して PDF の段落に追加します。
  • Document.Save() メソッドを使用して PDF ファイルを生成します。

次のコード スニペットは、Python で PDF ドキュメントを作成する方法を示しています。

# Initialize document object
document = aspose.pdf.Document()
# Add page
page = document.pages.add()
# Initialize textfragment object
text= "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." \
" Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris" \
" nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in " \
"reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur." \
" Excepteur sint occaecat cupidatat non proident, sunt in culpa qui " \
"officia deserunt mollit anim id est laborum."
text_fragment = aspose.pdf.text.TextFragment (text)
# Add text fragment to new page
page.paragraphs.add(text_fragment)
# Save updated PDF
document.save("output.pdf")

次のような出力が表示されます。

Python で PDF ドキュメントを作成する

Python{#python-apply-text-formatting-in-pdf } を使用して PDF にテキスト書式を適用する

ドキュメントを作成したら、Python PDF ライブラリを使用して PDF にテキストの書式を適用する方法を学びましょう。以下の手順に従ってください。

  • Document クラスを使用して新しい PDF ドキュメントを作成します。
  • テキストを配置するページを選択します。
  • TextFragment のオブジェクトを作成し、それらのテキストと、位置、フォント、色、サイズなどのその他の書式設定オプションを設定します。
  • Page.paragraphs.add() メソッドを使用して、テキスト フラグメントをページに追加します。
  • Document.Save() メソッドを呼び出して PDF ドキュメントを作成します。

次のコード スニペットは、Python を使用してプログラムで PDF にテキストの書式設定を適用する方法を示しています。

# Initialize document object
document = aspose.pdf.Document()
# Add page
page = document.pages.add()
# Create text fragments
text1= "Text1: Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." \
" Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris" \
" nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor"
text2= "Text2: Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
text_fragment1 = aspose.pdf.text.TextFragment (text1)
text_fragment1.position= aspose.pdf.text.Position(100, 700)
text_fragment2 = aspose.pdf.text.TextFragment (text2)
text_fragment2.position= aspose.pdf.text.Position(100, 600)
# Set text properties
text_fragment1.text_state.font_size = 12;
text_fragment1.text_state.font = aspose.pdf.text.FontRepository.find_font("TimesNewRoman");
text_fragment1.text_state.background_color=aspose.pdf.Color().aqua
text_fragment2.text_state.underline = True
text_fragment2.text_state.strike_out = True
# Add text fragments to page
page.paragraphs.add(text_fragment1)
page.paragraphs.add(text_fragment2)
# Save updated PDF
document.save("output.pdf")

次のような出力 PDF が表示されます。

Python を使用して PDF にテキスト書式を適用する

Python: プログラムで PDF ファイルに画像を挿入する

前のセクションで、テキストの書式設定を変更する方法を学びました。したがって、このセクションでは、PDF ドキュメントに画像を追加する方法について説明します。以下の手順に従って作業を行ってください。

  • Document クラスを使用して新しい PDF ドキュメントを作成します。
  • 画像を挿入したいページを取得します。
  • Page.add_image(file_path,rectangle) を使用してページに画像を追加しますが、Rectangle クラスを使用してページに画像を配置します。
  • Document.Save() メソッドを使用して PDF ドキュメントを生成します。

次のコード サンプルは、Python を使用して PDF に画像を追加する方法を示しています。

# Initialize document object
document = aspose.pdf.Document()
# Add page
page = document.pages.add()
# Set image coordinates
lowerLeftX = 400;
lowerLeftY = 400;
upperRightX = 150;
upperRightY = 150;
rectangle= aspose.pdf.Rectangle(lowerLeftX,lowerLeftY,upperRightX,upperRightY,True)
# Add image to page
page.add_image("aspose-logo.jpg", rectangle)
# Save updated PDF
document.save("output.pdf")

次のような出力 PDF が表示されます。

Python を使用して PDF ファイルに画像を挿入する

Python を使用して PDF にテーブルを追加する

画像を挿入したら、PDF ファイルに表を追加しましょう。以下の手順でコードを記述してください。

  • 新しい PDF を作成するには、Document クラスのオブジェクトを作成します。
  • テーブルを作成するページを取得します。
  • Table クラスのインスタンスを作成します。
  • BorderInfo クラスを使用して、テーブルとセルの境界線を指定します。
  • 新しい行を作成して Table.Rows コレクションに追加します。
  • Row.Cells コレクションにセルを追加します。
  • Page.paragraphs.add() メソッドを使用してページにテーブルを追加します。
  • Document.Save() メソッドを使用して PDF ドキュメントを保存します。

次のコード サンプルは、Python を使用して PDF ファイルにテーブルを追加する方法を示しています。

# Initialize document object
document = aspose.pdf.Document()
# Add page
page = document.pages.add()
# Create table
table = aspose.pdf.Table()
# Define table and cell borders
table.border=aspose.pdf.BorderInfo(aspose.pdf.BorderSide.ALL,2,aspose.pdf.Color().dark_gray)
table.default_cell_border= aspose.pdf.BorderInfo(aspose.pdf.BorderSide.ALL,2,aspose.pdf.Color().black)
# Add rows in the table
for i in range(1, 11):
row = table.rows.add()
# Add table cells
row.cells.add("Column (" + str(i) + ", 1)");
row.cells.add("Column (" + str(i) + ", 2)");
row.cells.add("Column (" + str(i) + ", 3)");
# Add table to the page
page.paragraphs.add(table);
# Save updated PDF
document.save("D:\\AsposeSampleResults\\output.pdf")

次のような出力 PDF が表示されます。

Python を使用して PDF にテーブルを追加する

無料ライセンスを取得する

無料の一時ライセンス を取得して、評価制限なしで API を試すことができます。

まとめ

この記事では、PythonでPDFファイルを作成する手順を説明しました。同様に、PDF ドキュメントに画像、表、テキストの書式を追加する方法についても説明しました。

ドキュメント にアクセスして、Python PDF クリエーター ライブラリ の詳細を確認してください。

conholdate.com で定期的な更新をお待ちください。

質問する

フォーラムでご質問やご質問をお知らせください。

関連項目