A library to generate Excel files from Python iterables
Project description
Introduction
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.
Licence
This package is covered by the permissive BSD licence.
Python versions
any2xl works on python 2.7 and python 3.4
Example Usage
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
Plans
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.
Changelog
0.1 Jul. 29 2015
Initial release
Contributors
By order of contribution date:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file any2xl-0.1.zip.
File metadata
- Download URL: any2xl-0.1.zip
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74c0d49d8ffa16d975cfe3984ebe1d42ee86c300b0f85ce8bea7cf2081bffb86
|
|
| MD5 |
f57032bf4f3d3cf9d351c88276242976
|
|
| BLAKE2b-256 |
997397e4474864f05854bb70f3d865debcdf1180dd5ce03166533f465786560f
|
File details
Details for the file any2xl-0.1.tar.gz.
File metadata
- Download URL: any2xl-0.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d42281c4653ec67570cc3a79043859f3f4a3c8388bf95f8a21658be02a806abe
|
|
| MD5 |
32b8d9f56217395cc596b471f72d07ee
|
|
| BLAKE2b-256 |
756a3da866d4b1dfb9ddb41c1a10895064be3f91676981166bbbfa8b0fef63c9
|
File details
Details for the file any2xl-0.1-py3.4.egg.
File metadata
- Download URL: any2xl-0.1-py3.4.egg
- Upload date:
- Size: 4.3 kB
- Tags: Egg
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2a39d95dfb7a21c1f1c96bef1edfa64c3f5905f1eeb57c6c9f81b37385d8c7f
|
|
| MD5 |
8e1714689566d60ac3b77ef9a9950cf6
|
|
| BLAKE2b-256 |
63aac0055abdda582df00321d813bc077de04a237d63edb8b072d2853ddceddf
|
File details
Details for the file any2xl-0.1-py3-none-any.whl.
File metadata
- Download URL: any2xl-0.1-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f807b14f6052cbbca33b8bb996f7b305738680f72cb9ab2b5125ddeb74ab9eb
|
|
| MD5 |
f3d096bb88d394e984e06b31e62b52a2
|
|
| BLAKE2b-256 |
eb6ed7ce9b89bb33ed77427f0799ea24922e222e261aa38c66cabde2439374d2
|