ワークシートを画像に変換 データ周りの余白を削除する
Contents
[
Hide
]
時には、ワークシートの画像をアプリケーションやWebページに表示する必要があります。たとえば、画像をWord文書、PDF、PowerPointプレゼンテーション、または他のドキュメントに挿入したい場合です。基本的に、ワークシートを画像としてレンダリングし、他のアプリケーションに貼り付けられるようにしたいのです。Aspose.Cells for Python via .NETは、Microsoft Excelのワークシートを画像に変換することができます。
データ周りの余白を削除してください
SheetRenderAPIは、ワークシートを指定された属性(たとえば、画像形式、ページ化されたシートなど)で画像ファイルに変換します。いくつかの画像形式がサポートされており、BMP、GIF、JPG、TIFF、EMFなどが含まれています。
シートを画像化する際、出力画像にはデフォルトで余白(ボーダー)があります。これを削除するには、元のワークシートの上部、下部、左側、右側のページ設定のマージンを0に設定し、それに応じてImageOrPrintOptions属性を指定してください。
次のコードスニペットは、出力画像のデータ周りの余白を削除します。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import LoadDataFilterOptions, LoadFilter, LoadOptions, PrintingPageType, Workbook | |
from aspose.cells.drawing import ImageType | |
from aspose.cells.rendering import ImageOrPrintOptions, SheetRender | |
# 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() | |
# Open the template file | |
book = Workbook(sourceDir + "Book1.xlsx") | |
# Get the first worksheet | |
sheet = book.worksheets[0] | |
options = LoadOptions() | |
options.load_filter = LoadFilter(LoadDataFilterOptions.ALL) | |
# Specify your print area if you want | |
# Sheet.PageSetup.PrintArea = "A1:H8"; | |
# To remove the white border around the image. | |
sheet.page_setup.left_margin = 0.0 | |
sheet.page_setup.right_margin = 0.0 | |
sheet.page_setup.bottom_margin = 0.0 | |
sheet.page_setup.top_margin = 0.0 | |
# Define ImageOrPrintOptions | |
imgOptions = ImageOrPrintOptions() | |
imgOptions.image_type = ImageType.EMF | |
# Set only one page would be rendered for the image | |
imgOptions.one_page_per_sheet = True | |
imgOptions.printing_page = PrintingPageType.IGNORE_BLANK | |
# Create the SheetRender object based on the sheet with its | |
# ImageOrPrintOptions attributes | |
sr = SheetRender(sheet, imgOptions) | |
# Convert the image | |
sr.to_image(0, outputDir + "outputRemoveWhitespaceAroundData.emf") |