A library to generate Excel files from Python iterables
any2xl is a helper module to ease producing XLS(X) files from different sources. It uses openpyxl by Eric Gazoni to produce the actual XLS file. It provides primitives and helpers alongside with any2 that help produce XLS(X) files from diverse sources.
This package is covered by the permissive BSD licence.
any2xl works on python 2.7 and python 3.4
from datetime import datetime as dt from decimal import Decimal from any2xl import List2xl target_filename = "out.xls" data = [ (dt.now(), Decimal("15.3"), u'Noël'), (dt.now(), Decimal("10.3"), u'Pentecôte'), (dt.now(), Decimal("100.02"), u'Jérôme'), (dt.now(), Decimal("0.03"), u'Some unaccented data'), ] colnames = ["Time", "Amount", "Description"] # we want column names as the first line of our XLS file # so we give the names to the constructor xl = List2xl(target_filename, colnames=colnames) # and we ask the write method to write the names xl.write(data, write_names=True) # serialize to disk xl.finalize()
In this example we only act as a really thin wrapper over the openpyxl library and if you only need this kind of functionality you may be better off using directly openpyxl…
The purpose of any2xl and were it is really interesting is when you have more complex datastructures:
from decimal import Decimal import datetime from any2xl import List2xl from any2 import Obj2List from any2 import NameTransformer quantizer = Decimal('0.01') class SubObj(object): def __init__(self, v): self.amount = Decimal('42.4242424242') self.start_date = datetime.date(year=2001, month=2, day=3) self.description = "%s_%s" % ("Task", v) class MyObj(object): def __init__(self, v, urgent): self.description = v self.urgent = urgent self.subobj = SubObj(v) def quantize2(value): return value.quantize(quantizer) def yesno(value): if value: return "Yes" else: return "No" vals = [('Project 1', True), ('Project 2', False), ('Project 3', False)] objs = [MyObj(*val) for val in vals] # the name transformer will work on output columns # in fact indexes... colnames = [ "Start Date", "Amount", "Description", "Task Description", "Is Urgent" ] transformer = NameTransformer(colnames) transformer.register_func('Amount', quantize2) transformer.register_func('Is Urgent', yesno) # to adapt an object as a list we must give the list of attributes we want attrs = [ 'subobj.start_date', 'subobj.amount', 'description', 'subobj.description', 'urgent' ] data_feed = Obj2List(objs, attrs, transformer=transformer) xl = List2xl('obj2list_out.xls', colnames=colnames) xl.write(data_feed, write_names=True) xl.finalize()
Here you see that we have a (somewhat complex) input iterator yielding imbricated objects and we need to transform some of the data during the process.
We could have used other transformers, the list is in any2.transformers
- Adding unit tests is our main priority, the main functionalities are in any2 which is 100% test covered. So we only need to add the thin wrapper above openpyxl in our unit tests
- At the moment we can only produce “raw” xls files without formatting. We plan to introduce new xl specific formatters to be able to apply cell and row formattings in the same way we use transformers.
0.1 Jul. 29 2015
- Initial release
By order of contribution date:
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, Size & Hash SHA256 Hash Help||File Type||Python Version||Upload Date|
(4.3 kB) Copy SHA256 Hash SHA256
|Egg||3.4||Jul 29, 2015|
(3.7 kB) Copy SHA256 Hash SHA256
|Wheel||3.4||Jul 29, 2015|
(3.9 kB) Copy SHA256 Hash SHA256
|Source||None||Jul 29, 2015|
(6.2 kB) Copy SHA256 Hash SHA256
|Source||None||Jul 29, 2015|