Printing Range of Pages using SheetRender and WorkbookRender
Microsoft Excel allows you to print range of pages of workbook or worksheet. The following screenshot shows the Microsoft Excel interface to specify the range of pages.
Aspose.Cells for Python via .NET provides the WorkbookRender.ToPrinter(string PrinterName, int PrintPageIndex, int PrintPageCount) and SheetRender.ToPrinter(string PrinterName, int PrintPageIndex, int PrintPageCount) methods for this purpose.
Microsoft Excel Interface to specify the Range of Pages to Print
The following sample code illustrates the use of WorkbookRender.ToPrinter(string PrinterName, int PrintPageIndex, int PrintPageCount) and SheetRender.ToPrinter(string PrinterName, int PrintPageIndex, int PrintPageCount) methods. It prints the pages 2-5 of the workbook and worksheet.
from aspose.cells import Workbook | |
from aspose.cells.rendering import ImageOrPrintOptions, SheetRender, WorkbookRender | |
# 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(".") | |
# Create workbook from source Excel file | |
workbook = Workbook(dataDir + "SampleBook.xlsx") | |
printerName = "" | |
while null == printerName or "" == printerName: | |
print("Please Enter Your Printer Name:") | |
printerName = input() | |
bookRenderOptions = ImageOrPrintOptions() | |
bookRenderOptions.page_index = 1 | |
bookRenderOptions.page_count = 2 | |
# Print the worbook specifying the range of pages. Here we are printing pages 2-3 | |
wr = WorkbookRender(workbook, ImageOrPrintOptions()) | |
try: | |
wr.to_printer(printerName) | |
except Exception as ex: | |
print(str(ex)) | |
# Access first worksheet | |
worksheet = workbook.worksheets[0] | |
sheetRenderOptions = ImageOrPrintOptions() | |
sheetRenderOptions.page_index = 1 | |
sheetRenderOptions.page_count = 2 | |
# Print the worksheet specifying the range of pages. Here we are printing pages 2-3 | |
sr = SheetRender(worksheet, ImageOrPrintOptions()) | |
try: | |
sr.to_printer(printerName) | |
except Exception as ex: | |
print(str(ex)) |