Difference between revisions of "Python/PrintPagesRanges"
Marcoagpinto (Talk | contribs) ([Calc, Python] Print selected sheets and ranges) |
(Add steps and some extra documentation) |
||
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]. | + | This script was published by Villeroy at the [http://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=62054 Code Snippets Forum]. |
+ | ;Villeroy quote: | ||
+ | :''The attached Python code provides 2 routines. One prints the current selection of (multiple) cell range(s), the other one prints the used ranges of the selected sheet(s).'' | ||
== Original code == | == Original code == | ||
Line 14: | Line 16: | ||
return oRg.getRangeAddress() | return oRg.getRangeAddress() | ||
− | def clearPrintAreas(doc): | + | 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): | def getPrintAreasDict(doc): | ||
'''return a dict of sheet indices with print areas''' | '''return a dict of sheet indices with print areas''' | ||
Line 99: | Line 96: | ||
== Process of the script == | == Process of the script == | ||
+ | The script perform the following tasks: | ||
+ | # Loop through the sheets collection and load the print ranges them in a variable. | ||
+ | # Remove them | ||
+ | # Change the selected ranges into print ranges | ||
+ | # Store the ranges into a temporary PDF | ||
+ | # Clear the print ranges | ||
+ | # Restore the original print ranges | ||
+ | |||
+ | == Load print ranges == | ||
+ | |||
+ | <source lang="Python"> | ||
+ | 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() | ||
+ | </source> | ||
+ | |||
+ | == Clear print ranges === | ||
+ | |||
+ | <source lang="Python> | ||
+ | def clearPrintAreas(doc): '''remove all print areas from a Calc document''' esh = doc.Sheets.createEnumeration() while esh.hasMoreElements(): sh = esh.nextElement() sh.setPrintAreas(tuple())</source> | ||
+ | |||
+ | == Create print ranges == | ||
+ | |||
+ | <source lang="Python> | ||
+ | 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)</source> | ||
+ | |||
+ | == Print as PDF == | ||
+ | |||
+ | <source lang="Python> | ||
+ | def getTmpURL(): | ||
+ | import unohelper, os | ||
+ | f = os.tmpnam() +'.pdf' | ||
+ | return unohelper.systemPathToFileUrl(f)</source> | ||
+ | |||
+ | == Clear the ranges == | ||
+ | |||
+ | <source lang="Python> | ||
+ | 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</source> | ||
+ | == Restore print ranges == | ||
+ | <source lang="Python></source> | ||
[[Category:Python]] | [[Category:Python]] |
Revision as of 22:07, 25 August 2013
This script was published by Villeroy at the Code Snippets Forum.
- Villeroy quote
- The attached Python code provides 2 routines. One prints the current selection of (multiple) cell range(s), the other one prints the used ranges of the selected sheet(s).
Contents
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,
Process of the script
The script perform the following tasks:
- Loop through the sheets collection and load the print ranges them in a variable.
- Remove them
- Change the selected ranges into print ranges
- Store the ranges into a temporary PDF
- Clear the print ranges
- Restore the original print ranges
Load print ranges
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()
Clear print ranges =
Invalid language.
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic
def clearPrintAreas(doc): '''remove all print areas from a Calc document''' esh = doc.Sheets.createEnumeration() while esh.hasMoreElements(): sh = esh.nextElement() sh.setPrintAreas(tuple())
Create print ranges
Invalid language.
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic
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)
Print as PDF
Invalid language.
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic
def getTmpURL(): import unohelper, os f = os.tmpnam() +'.pdf' return unohelper.systemPathToFileUrl(f)
Clear the ranges
Invalid language.
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic
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
Restore print ranges
Invalid language.
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic