Not really Dabo related, but it might save someone some coding.
Sorry the comments are in spanish.
You need to import xlsxwriter. When I write about column formats below
you will have to check xlsxwriter's documentation to get all the
possible formats, but I am including a few examples below.
You send the function two parameters, the excel file's name (nom) and a
list of page definitions (pags).
The page definitions are also lists consisting of the page's name
(npag1), the dataSet to be listed in that page (ds1), and a list of
column definitions for every column of the dataSet (cols1).
The column definitions are lists consisting of the column's title, the
name of the field of the dataSet that will go in this column, and a
dictionary of format settings for the column.
The title row will have a double width, a yellow background, and
autoFilter buttons, and it will be fixed so that when you scroll the
data the titles will still be there. If there are more than 64,000 rows
in the sheet then a second sheet named "firstSheetName 2" will be
created with the same title settings and with the rest of the rows (as
many extra sheets as are needed).
This a usage example, it will produce an excel file with two sheets, the
first sheet will list the "Tarjeta" bizObj, and the second sheet will
list the "Movimientos" bizObj filtered to show the last six months only:
nom = 'WinProx.xlsx'
npag1 = 'Tarjetas'
biz = self.getBizobj('Tarjeta')
ds1 = biz.bizDataIterator(restorePointer=True)
cols1 = (('Nro Tarjeta', 'NroTarjeta', {'bold': True,
'align': 'right'}),
('Apellido', 'Apellido', {}),
('Nombre', 'Nombre', {}),
('DNI', 'DNI', {'num_format': '#,##0'}),
('Domicilio', 'Domicilio', {'text_wrap': True}),
('Localidad', 'Localidad', {}),
('Nº Ext.', 'GrupoId', {'num_format': '#,##0'}),
('Grupo Acceso', 'Grupo', {}),
('Estado', 'Estado', {}))
npag2 = 'Movimientos'
ddeFecha = datetime.datetime.now() - datetime.timedelta(days=183)
biz = self.getBizobj('Movimiento')
ds2 = biz.getDataSet()
ds2.filter('FechaHora', ddeFecha, '>=')
cols2 = (('Fecha', 'FechaHora', {'bold': True,
'num_format': 'dd/mm/yyyy
hh:mm'}),
('Nro Tarjeta', 'NroTarjeta', {'bold': True,
'align': 'right'}),
('Nombre', 'FullName', {'text_wrap': True}),
('Puerta', 'Puerta', {}),
('Evento', 'Evento', {}),
('Grupo de Acceso', 'Grupo', {}))
pags = ((npag1, ds1, cols1),
(npag2, ds2, cols2))
self.Application.lib.genPlanilla(nom, pags)
This is the function's code :
---------------------------------------------------------------------------------------------------------------
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 5 17:09:26 2013
@author: richie
"""
import xlsxwriter as xls
def _titulos(wbk, ws, columnas):
titFmt = wbk.add_format({'bold': True,
'bg_color': 'yellow',
'valign': 'top'})
for (col, (nombreCol, nombreFld, formato)) in enumerate(columnas):
ws.write(0, col, nombreCol.capitalize(), titFmt)
colFmt = wbk.add_format(formato)
ws.set_column(col, col, None, colFmt)
ws.set_row(0, ws.default_row_height * 2)
ws.freeze_panes(1, 0)
ws.autofilter(0, 0, 0, len(columnas) - 1)
ws.set_selection(1, 0, 1, 0)
def _linea(wbk, ws, nroLinea, columnas, data):
for (nroCol, (nombreCol, nombreFld, formato)) in enumerate(columnas):
ws.write(nroLinea, nroCol, data[nombreFld])
def genPlanilla(nombre, paginas):
""" genPlanilla(nombre, paginas)
nombre: Nombre del archivo xlsx (se le agrega extensión si no tiene)
paginas: lista de tuplas de la forma (nomPag, ds, cols) donde:
nomPag: Nombre de la página (el que aparece en la solapa)
ds: dataSet (clase de Dabo)
cols: lista de duplas (nom, fld, fmt) para cada columna donde:
nom: Nombre de la columna
fld: Nombre del campo (en el DataSet)
fmt: formato, diccionario de propiedades de formato
para
esa columna, por ej:
{'bold': True,
'bg_color': 'yellow',
'valign': 'top',
'num_format': '$#,##0.00',
'set_indent': 2} o {} si no hay formato
Nota: si un dataSet tiene más de 64000 líneas entonces se agrega otra
página con el mismo nombre que la anterior mas el número, por
ejemplo : 'mi nombre página', 'mi nombre página 2'
"""
wbk = xls.Workbook(nombre)
for (nombrePag, dataSet, columnas) in paginas:
num_pag = 1
ws = wbk.add_worksheet(nombrePag)
_titulos(wbk, ws, columnas)
for (linea, record) in enumerate(dataSet, 1):
_linea(wbk, ws, linea, columnas, record)
if linea >= 64000:
num_pag += 1
ws = wbk.add_worksheet(nombrePag + str(num_pag))
_titulos(wbk, ws, columnas)
wbk.close()
wbk = None
-------------------------------------------------------------------------------------------------------------------------
_______________________________________________
Post Messages to: Dabo-users@leafe.com
Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/527d2ae6.10...@gmail.com