Print and Preview workbook

Microsoft Excel assumes that you want to print the entire worksheet area unless you specify a selection. To print using Aspose.Cells for Python via .NET, first import the aspose.cells.rendering namespace to the program. It has several useful classes, for example, SheetRender and WorkbookRender.

The SheetRender class represents a worksheet and has the to_printer method which can print a worksheet. The following sample code shows how to print a worksheet.

from aspose.cells import PrintingPageType, Workbook
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
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiate a workbook with Excel file.
workbook = Workbook(dataDir + "SampleBook.xlsx")
printerName = ""
while null == printerName or "" == printerName:
print("Please Enter Your Printer Name:")
printerName = input()
# Get the second sheet.
worksheet = workbook.worksheets[1]
# Apply different Image/Print options.
options = ImageOrPrintOptions()
options.printing_page = PrintingPageType.DEFAULT
sr = SheetRender(worksheet, options)
print("Printing SampleBook.xlsx")
# Print the sheet.
try:
sr.to_printer(printerName)
print("Pinting finished.")
except Exception as ex:
print(str(ex))

To print a whole workbook, iterate through the sheets and print them, or use the WorkbookRender class.

from aspose.cells import PrintingPageType, Workbook
from aspose.cells.drawing import ImageType
from aspose.cells.rendering import ImageOrPrintOptions, WorkbookRender
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Instantiate a workbook with an Excel file.
workbook = Workbook(sourceDir + "samplePrintingUsingWorkbookRender.xlsx")
printerName = "doPDF 8"
# Apply different Image/Print options.
options = ImageOrPrintOptions()
options.image_type = ImageType.TIFF
options.printing_page = PrintingPageType.DEFAULT
# To print a whole workbook, iterate through the sheets and print them, or use the WorkbookRender class.
wr = WorkbookRender(workbook, options)
try:
# Print the workbook.
wr.to_printer(printerName)
except Exception as ex:
print(str(ex))

There may be cases where Excel files with millions of pages need to be converted to PDF or images. Processing such files will consume a lot of time and resources. In such cases, the Workbook and Worksheet Print Preview feature might prove to be useful. Before converting such files, the user can check the total number of pages and then decide whether the file is to be converted or not. This article focuses on using the WorkbookPrintingPreview and SheetPrintingPreview classes to find out the total number of pages.

Aspose.Cells for Python via .NET provides the print preview feature. For this, the API provides WorkbookPrintingPreview and SheetPrintingPreview classes. To create the print preview of the whole workbook, create an instance of the WorkbookPrintingPreview class by passing Workbook and ImageOrPrintOptions objects to the constructor. The WorkbookPrintingPreview class provides an evaluated_page_count method which returns the number of pages in the generated preview. Similar to WorkbookPrintingPreview class, the SheetPrintingPreview class is used to generate a print preview for a specific worksheet. To create the print preview of a worksheet, create an instance of the SheetPrintingPreview class by passing Worksheet and ImageOrPrintOptions objects to the constructor. The SheetPrintingPreview class also provides an SheetPrintingPreview.evaluated_page_count method which returns the number of pages in the generated preview.

The following code snippet demonstrates the use of both WorkbookPrintingPreview and SheetPrintingPreview classes by using the sample excel file.

Sample Code

from aspose.cells import Workbook
from aspose.cells.rendering import ImageOrPrintOptions, SheetPrintingPreview, WorkbookPrintingPreview
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
workbook = Workbook(sourceDir + "Book1.xlsx")
imgOptions = ImageOrPrintOptions()
preview = WorkbookPrintingPreview(workbook, imgOptions)
print("Workbook page count: " + str(preview.evaluated_page_count))
preview2 = SheetPrintingPreview(workbook.worksheets[0], imgOptions)
print("Worksheet page count: " + str(preview2.evaluated_page_count))

The following is the output generated by executing the above code.

Console Output

Workbook page count: 1
Worksheet page count: 1