Help Calc.print_sheet() method
Overview
Sometimes it is desirable to print a range of a spreadsheet document programmatically.
The Calc.print_sheet()
method can be used to accomplish this task.
Print a range of a spreadsheet document
Pass parameters directly
Parameters a passed directly to print_sheet()
method.
import uno
from ooodev.office.calc import Calc
# ... other code
# open document
doc = Calc.open_doc(fnm="my_spreadsheet.ods")
# print range C6:G33 of the first sheet,
# optionally idx value can be used to print from sheet matching idx number.
Calc.print_sheet(printer_name="Brother MFC-L2750DW series", range_name="C6:G33")
Get the parameters from the user defined properties
Using Info.get_user_defined_props
method we can get the
user defined properties seen in Fig. 1188 of the document.
The document custom properties contains the sheet index value to print and the printer name.
Using the Calc.find_used_range()
method we can get the used range of of the sheet.
import uno
from com.sun.star.beans import XPropertySet
from ooodev.loader.lo import Lo
from ooodev.office.calc import Calc
from ooodev.utils.info import Info
# ... other code
doc = Calc.open_doc(fnm="my_spreadsheet.ods")
user_props = Info.get_user_defined_props(doc)
# get properties as XPropertySet
ps = Lo.qi(XPropertySet, user_props, True)
idx = int(ps.getPropertyValue("PrintSheet")) -1 # -1 because index starts at 0
printer_name = ps.getPropertyValue("PrinterName")
sheet = Calc.get_sheet(doc=doc, idx=idx)
# find the used range of the sheet
used_range = Calc.find_used_range(sheet=sheet) # XCellRange
# send the used range to the printer
Calc.print_sheet(printer_name=printer_name, cell_range=used_range)