Pdf

ExcelワークブックをPDFに変換する

PDFファイルは、組織、政府部門、個人間で文書を交換するために広く使用されています。これは標準のドキュメント形式であり、ソフトウェア開発者はしばしばMicrosoft ExcelファイルをPDFドキュメントに変換する方法を見つけるよう求められます。

Aspose.Cells for Python via .NETは、高いビジュアルの忠実度を保ちながらExcelファイルをPDFに変換することをサポートしています。

直接変換

Aspose.Cells for Python via .NETは、他のソフトウェアに依存せずにスプレッドシートをPDFに変換することをサポートしています。ネイティブのExcelファイルをPDF形式に変換するためのsaveメソッドを使用してExcelファイルをPDFに保存する。

以下の手順に従って、Excelスプレッドシートを直接PDF形式に変換します:

  1. 空のコンストラクタを呼び出してWorkbookクラスのオブジェクトをインスタンス化します。
  2. 既存のテンプレートファイルを開いたり読み込んだりするか、ワークブックをゼロから作成している場合は、この手順をスキップします。
  3. Aspose.Cells for Python via .NETのAPIを使用してスプレッドシートで作業を行います(入力データを設定、書式を適用、数式を設定、画像やその他の図形オブジェクトを挿入など)。
  4. スプレッドシートのコードが完了したら、Workbookクラスのsaveメソッドを呼び出してスプレッドシートを保存します。

ファイル形式はPDFである必要があるため、最終的なPDFドキュメントを生成するためにSaveFormat(事前定義された値)から選択します。

from aspose.cells import SaveFormat, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiate the Workbook object
# Open an Excel file
workbook = Workbook(dataDir + "Book1.xls")
# Save the document in PDF format
workbook.save(dataDir + "output.pdf", SaveFormat.PDF)

高度な変換

PdfSaveOptionsクラスを使用して、変換のためにさまざまな属性を設定することもできます。PdfSaveOptionsクラスのさまざまなプロパティを設定すると、出力PDFの印刷、フォント、セキュリティ、圧縮設定を制御できます。最も重要なプロパティはPdfSaveOptions.complianceで、このプロパティを使用するとExcelファイルをPDF/A準拠のPDFファイルに保存できます。

PDF/A準拠ファイルへのワークブックの保存

以下のコードスニペットは、PdfSaveOptions クラスを使用してExcelファイルをPDF/A準拠のPDF形式に保存する方法を示しています。

from aspose.cells import PdfSaveOptions, Workbook
from aspose.cells.rendering import PdfCompliance
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiate new workbook
workbook = Workbook()
# Insert a value into the A1 cell in the first worksheet
workbook.worksheets[0].cells.get(0, 0).put_value("Testing PDF/A")
# Define PdfSaveOptions
pdfSaveOptions = PdfSaveOptions()
# Set the compliance type
pdfSaveOptions.compliance = PdfCompliance.PDF_A1B
# Save the file
workbook.save(dataDir + "output.pdf", pdfSaveOptions)

PDF作成時間の設定

PdfSaveOptions クラスを使用すると、PDF作成時刻を取得または設定することができます。次のコードは、PdfSaveOptions.created_time プロパティを使用してPDFファイルの作成時刻を設定する方法を示しています。

from aspose.cells import PdfSaveOptions, SaveFormat, Workbook
from datetime import datetime
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
dataDir = RunExamples.GetDataDir(".")
inputPath = dataDir + "Book1.xlsx"
# Load excel file containing charts
workbook = Workbook(inputPath)
# Create an instance of PdfSaveOptions and pass SaveFormat to the constructor
options = PdfSaveOptions(SaveFormat.PDF)
options.created_time = datetime.now()
# Save the workbook to PDF format while passing the object of PdfSaveOptions
workbook.save(dataDir + "output.pdf", options)

ContentCopyForAccessibilityオプションの設定

PdfSaveOptions クラスを使用すると、変換されたPDFのコンテンツアクセスを制御するためのPDF PdfSecurityOptions.accessibility_extract_content オプションを取得または設定できます。

from aspose.cells import PdfSaveOptions, SaveFormat, Workbook
from aspose.cells.rendering.pdfsecurity import PdfSecurityOptions
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
inputPath = sourceDir + "BookWithSomeData.xlsx"
# Load excel file containing some data
workbook = Workbook(inputPath)
# Create an instance of PdfSaveOptions and pass SaveFormat to the constructor
pdfSaveOpt = PdfSaveOptions(SaveFormat.PDF)
# Create an instance of PdfSecurityOptions
securityOptions = PdfSecurityOptions()
# Set AccessibilityExtractContent to true
securityOptions.accessibility_extract_content = False
# Set the securityoption in the PdfSaveOptions
pdfSaveOpt.security_options = securityOptions
# Save the workbook to PDF format while passing the object of PdfSaveOptions
workbook.save(outputDir + "outFile.pdf", pdfSaveOpt)

PDFへのカスタムプロパティのエクスポート

PdfSaveOptions クラスを使用すると、元のワークブック内のカスタムプロパティをPDFにエクスポートすることができます。プロパティのエクスポート方法を指定するために PdfCustomPropertiesExport 列挙型が提供されています。これらのプロパティは、次の画像に示すように、Adobe Acrobat Readerで[ファイル]をクリックして[プロパティ]オプションをクリックすることで観察することができます。テンプレートファイル “sourceWithCustProps.xlsx” はこちらからダウンロードでき、解析用の出力PDFファイル “outSourceWithCustProps” はこちらで利用できます。

todo:image_alt_text

from aspose.cells import PdfSaveOptions, SaveFormat, Workbook
from aspose.cells.rendering import PdfCustomPropertiesExport
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Load excel file containing custom properties
workbook = Workbook("sourceWithCustProps.xlsx")
# Create an instance of PdfSaveOptions and pass SaveFormat to the constructor
pdfSaveOptions = PdfSaveOptions(SaveFormat.PDF)
# Set CustomPropertiesExport property to PdfCustomPropertiesExport.Standard
pdfSaveOptions.custom_properties_export = PdfCustomPropertiesExport.STANDARD
# Save the workbook to PDF format while passing the object of PdfSaveOptions
workbook.save("outSourceWithCustProps.pdf", pdfSaveOptions)

変換属性

新しいリリースごとに変換機能を強化しています。Aspose.CellのExcelからPDFへの変換にはまだいくつかの制限があります。MapChartはPDF形式への変換時にサポートされていません。また、一部の図形オブジェクトには十分なサポートがありません。

以下の表は、Aspose.Cells for Python via .NETを使用してPDFに変換する際に完全または一部サポートされている機能を示しています。この表は最終版ではなく、スプレッドシート属性のすべてを網羅していませんが、PDFへの変換において完全にサポートされていないまたは部分的にサポートされている機能を識別しています。

ドキュメント要素 属性 サポート 注釈
配置 はい
背景設定 はい
ボーダー はい
ボーダー 線のスタイル はい
ボーダー 線の幅 はい
セルデータ はい
コメント はい
条件付き書式 はい
ドキュメントプロパティ はい
図形オブジェクト 部分的 図形オブジェクトの影や3D効果には十分なサポートがありません。WordArtとSmartArtは部分的にサポートされています。
フォント サイズ はい
フォント はい
フォント スタイル はい
フォント 下線 はい
フォント 効果 はい
画像 はい
ハイパーリンク はい
チャート 部分的に MapChartはサポートされていません。
セルの結合 はい
改ページ はい
ページ設定 ヘッダー/フッター はい
ページ設定 余白 はい
ページ設定 ページの向き はい
ページ設定 ページサイズ はい
ページ設定 印刷範囲 はい
ページ設定 印刷タイトル はい
ページ設定 拡大/縮小 はい
行の高さ/列の幅 はい
右から左への言語 はい

高度なトピック