Skip to main content

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:

Release files for any2xl 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distributions (sdists)

Source distribution for any2xl 0.1
File Size Uploaded
any2xl-0.1.zip 6.2 kB Details
any2xl-0.1.tar.gz 3.9 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for any2xl 0.1
File Interpreter ABI Platform
any2xl-0.1-py3.4.egg Legacy Egg format - - Details
any2xl-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 18.1 kB

Release files / any2xl-0.1.zip

Download URL any2xl-0.1.zip
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
74c0d49d8ffa16d975cfe3984ebe1d42ee86c300b0f85ce8bea7cf2081bffb86
BLAKE2b-256 checksum
How to use checksums
997397e4474864f05854bb70f3d865debcdf1180dd5ce03166533f465786560f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / any2xl-0.1.tar.gz

Download URL any2xl-0.1.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d42281c4653ec67570cc3a79043859f3f4a3c8388bf95f8a21658be02a806abe
BLAKE2b-256 checksum
How to use checksums
756a3da866d4b1dfb9ddb41c1a10895064be3f91676981166bbbfa8b0fef63c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / any2xl-0.1-py3.4.egg

Download URL any2xl-0.1-py3.4.egg
Size 4.3 kB
Tags Egg
SHA-256 checksum
How to use checksums
b2a39d95dfb7a21c1f1c96bef1edfa64c3f5905f1eeb57c6c9f81b37385d8c7f
BLAKE2b-256 checksum
How to use checksums
63aac0055abdda582df00321d813bc077de04a237d63edb8b072d2853ddceddf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / any2xl-0.1-py3-none-any.whl

Download URL any2xl-0.1-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f807b14f6052cbbca33b8bb996f7b305738680f72cb9ab2b5125ddeb74ab9eb
BLAKE2b-256 checksum
How to use checksums
eb6ed7ce9b89bb33ed77427f0799ea24922e222e261aa38c66cabde2439374d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1 This release

4 release files

0.1.dev1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page