跳转到主要内容

一个用于从Python可迭代对象生成Excel文件的库

项目描述

简介

any2xl 是一个辅助模块,用于简化从不同来源生成XLS(X)文件。它使用 openpyxl 由Eric Gazoni生成实际的XLS文件。它提供了与 any2 一起使用的原语和辅助工具,帮助从不同的来源生成XLS(X)文件。

许可证

本软件包受宽松的BSD许可证保护。

Python版本

any2xl 在python 2.7和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()

在此示例中,我们仅作为openpyxl库的真正薄的包装器,如果您只需要此类功能,您可能最好直接使用openpyxl…

any2xl的用途及其真正有趣之处在于当您有更复杂的数据结构时

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()

在这里,您可以看到我们有一个(有些复杂)的输入迭代器,它产生嵌套的对象,并且在处理过程中我们需要转换一些数据。

我们可以使用其他转换器,列表在any2.transformers中

计划

  • 添加单元测试是我们的主要优先事项,主要功能在 any2 中,它100%经过测试。所以我们只需要在我们的单元测试中添加上面的openpyxl的薄包装器

  • 目前我们只能生成“原始”xls文件,没有格式。我们计划引入新的xl特定格式化程序,以便能够以我们使用转换器的方式应用单元格和行格式。

更新日志

0.1 2015年7月29日

  • 初始发布

贡献者

按贡献日期排序

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

any2xl-0.1.zip (6.2 kB 查看散列值)

上传时间:

any2xl-0.1.tar.gz (3.9 kB 查看散列值)

上传时间:

构建分布

any2xl-0.1-py3.4.egg (4.3 kB 查看散列值)

上传时间:

any2xl-0.1-py3-none-any.whl (3.7 kB 查看散列值)

上传时间: Python 3

由以下支持