Difference between revisions of "Python/PrintPagesRanges"
From Apache OpenOffice Wiki
< Python
Marcoagpinto (talk | contribs) (How to print selected sheets or selected cells only.) |
Marcoagpinto (talk | contribs) ([Calc, Python] Print selected sheets and ranges) |
||
Line 1: | Line 1: | ||
{{DISPLAYTITLE:Printing selected sheets and ranges}} | {{DISPLAYTITLE:Printing selected sheets and ranges}} | ||
− | + | This script was published by Villeroy at the [http://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=62054 Code Snippets Forum]." | |
== Original code == | == Original code == |
Revision as of 21:45, 25 August 2013
This script was published by Villeroy at the Code Snippets Forum."
Original code
import uno
from com.sun.star.uno import RuntimeException
def getUsedAddress(oSheet):
'''com.sun.star.table.CellRangeAddress of a sheet's used range'''
oRg = oSheet.createCursor()
oRg.gotoStartOfUsedArea(False)
oRg.gotoEndOfUsedArea(True)
return oRg.getRangeAddress()
def clearPrintAreas(doc):
'''remove all print areas from a Calc document'''
esh = doc.Sheets.createEnumeration()
while esh.hasMoreElements():
sh = esh.nextElement()
sh.setPrintAreas(tuple())
def getPrintAreasDict(doc):
'''return a dict of sheet indices with print areas'''
d = {}
esh = doc.Sheets.createEnumeration()
while esh.hasMoreElements():
sh = esh.nextElement()
n = sh.RangeAddress.Sheet
d[n]= sh.getPrintAreas()
return d
def setPrintAreasByDict(doc, d, bWholeSheet):
for k,v in d.items():
sh = doc.Sheets.getByIndex(k)
if bWholeSheet:
v = (getUsedAddress(sh),)
sh.setPrintAreas(v)
def getTmpURL():
import unohelper, os
f = os.tmpnam() +'.pdf'
return unohelper.systemPathToFileUrl(f)
def getDictFromAddresses(tpla):
'''Split tuple of addresses into dict of sheet indices.'''
d = {}
for i in tpla:
ish = i.Sheet
if not d.has_key(ish):
d[ish] = [i,]
else:
d[ish].append(i)
for k,v in d.items():
d[k] = tuple(v)
return d
def printSelectedCells():
'''Print current selection of (multiple) cell range(s)'''
printSomething(False)
def printSelectedSheets():
'''Print used ranges of currently selected sheets'''
printSomething(True)
def printSomething(bWholeSheet):
doc = XSCRIPTCONTEXT.getDocument()
sel = doc.getCurrentSelection()
# Let's support c.s.s.sheet.SheetCellRanges collection.
# The intersection of a range with its own address
# gives a collection having that single range
if sel.supportsService('com.sun.star.sheet.SheetCellRange'):
sel = sel.queryIntersection(sel.getRangeAddress())
elif sel.supportsService('com.sun.star.sheet.SheetCellRanges'):
a = sel.getRangeAddresses()
else:
raise(Exception, 'NO RANGE SELECTION')
# back up current print areas
d1 = getPrintAreasDict(doc)
clearPrintAreas(doc)
d2 = getDictFromAddresses(a)
setPrintAreasByDict(doc, d2, bWholeSheet)
p = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
p.Name = 'FileName'
p.Value = getTmpURL()
doc.com_sun_star_view_XPrintable_print((p,))
clearPrintAreas(doc)
setPrintAreasByDict(doc, d1, False)
g_exportedScripts = printSelectedCells, printSelectedSheets,