Skip to main content

以 xlsx 和 xlsmformat 读取、操作和写入数据的包装库

项目描述

https://raw.githubusercontent.com/pyexcel/pyexcel.github.io/master/images/patreon.png https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg https://travis-ci.org/pyexcel/pyexcel-xlsx.svg?branch=master https://codecov.io/gh/pyexcel/pyexcel-xlsx/branch/master/graph/badge.svg https://badge.fury.io/py/pyexcel-xlsx.svg https://anaconda.org/conda-forge/pyexcel-xlsx/badges/version.svg https://pepy.tech/badge/pyexcel-xlsx/month https://anaconda.org/conda-forge/pyexcel-xlsx/badges/downloads.svg https://img.shields.io/gitter/room/gitterHQ/gitter.svg https://img.shields.io/static/v1?label=continuous%20template&message=%E6%A8%A1%E7%89%88%E6%9B%B4%E6%96%B0&color=blue&style=flat-square https://img.shields.io/static/v1?label=coding%20style&message=black&color=black&style=flat-square

pyexcel-xlsx是一个小型包装库,用于读取、操作和写入 xlsx 和 xlsm 格式的数据,使用 来自 openpyxl 的read_only模式读取器和write_only模式写入器。您可能会将其与pyexcel一起使用。

请注意:

  1. auto_detect_int标志不会生效,因为 openpyxl 默认在 python 3 中检测整数。

  2. skip_hidden_​​row_and_column将在无法使用read_only模式的情况下受到惩罚。

支持项目

如果贵公司已将 pyexcel 及其组件嵌入到创收产品中,请在 github、patreonbounty 源上支持我,以维护项目并进一步开发。

如果您是个人,也欢迎您支持我,无论您愿意多久。作为我的支持者,您将获得 pyexcel 相关内容的早期访问权限

如果您想成为我的pyexcel 专业用户的 patreon,您的问题将得到优先考虑。

有了您的经济支持,我将能够在编码、文档和撰写有趣的帖子上投入更多时间。

已知约束

不支持字体、颜色和图表。

安装

您可以通过 pip 安装 pyexcel-xlsx:

$ pip install pyexcel-xlsx

或克隆它并安装它:

$ git clone https://github.com/pyexcel/pyexcel-xlsx.git
$ cd pyexcel-xlsx
$ python setup.py install

用法

作为一个独立的库

写入 xlsx 文件

这是将字典写入 xlsx 文件的示例代码:

>>> from pyexcel_xlsx import save_data
>>> data = OrderedDict() # from collections import OrderedDict
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
>>> data.update({"Sheet 2": [["row 1", "row 2", "row 3"]]})
>>> save_data("your_file.xlsx", data)

从 xlsx 文件中读取

这是示例代码:

>>> from pyexcel_xlsx import get_data
>>> data = get_data("your_file.xlsx")
>>> import json
>>> print(json.dumps(data))
{"Sheet 1": [[1, 2, 3], [4, 5, 6]], "Sheet 2": [["row 1", "row 2", "row 3"]]}

将 xlsx 写入内存

这是将字典写入 xlsx 文件的示例代码:

>>> from pyexcel_xlsx import save_data
>>> data = OrderedDict()
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
>>> data.update({"Sheet 2": [[7, 8, 9], [10, 11, 12]]})
>>> io = StringIO()
>>> save_data(io, data)
>>> # do something with the io
>>> # In reality, you might give it to your http response
>>> # object for downloading

从内存中读取 xlsx

继续上一个示例:

>>> # This is just an illustration
>>> # In reality, you might deal with xlsx file upload
>>> # where you will read from requests.FILES['YOUR_XLSX_FILE']
>>> data = get_data(io)
>>> print(json.dumps(data))
{"Sheet 1": [[1, 2, 3], [4, 5, 6]], "Sheet 2": [[7, 8, 9], [10, 11, 12]]}

分页功能

假设以下文件是一个巨大的 xlsx 文件:

>>> huge_data = [
...     [1, 21, 31],
...     [2, 22, 32],
...     [3, 23, 33],
...     [4, 24, 34],
...     [5, 25, 35],
...     [6, 26, 36]
... ]
>>> sheetx = {
...     "huge": huge_data
... }
>>> save_data("huge_file.xlsx", sheetx)

让我们假装读取部分数据:

>>> partial_data = get_data("huge_file.xlsx", start_row=2, row_limit=3)
>>> print(json.dumps(partial_data))
{"huge": [[3, 23, 33], [4, 24, 34], [5, 25, 35]]}

你也可以对列做同样的事情:

>>> partial_data = get_data("huge_file.xlsx", start_column=1, column_limit=2)
>>> print(json.dumps(partial_data))
{"huge": [[21, 31], [22, 32], [23, 33], [24, 34], [25, 35], [26, 36]]}

很明显,你可以同时做这两个:

>>> partial_data = get_data("huge_file.xlsx",
...     start_row=2, row_limit=3,
...     start_column=1, column_limit=2)
>>> print(json.dumps(partial_data))
{"huge": [[23, 33], [24, 34], [25, 35]]}

作为一个pyexcel插件

不再需要显式导入,因为 pyexcel 版本 0.2.2。相反,这个库是自动加载的。所以如果你想读取xlsx格式的数据,安装就够了。

从 xlsx 文件中读取

这是示例代码:

>>> import pyexcel as pe
>>> sheet = pe.get_book(file_name="your_file.xlsx")
>>> sheet
Sheet 1:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
Sheet 2:
+-------+-------+-------+
| row 1 | row 2 | row 3 |
+-------+-------+-------+

写入 xlsx 文件

这是示例代码:

>>> sheet.save_as("another_file.xlsx")

从 IO 实例中读取

你必须用流包装二进制内容才能使 xlsx 工作:

>>> # This is just an illustration
>>> # In reality, you might deal with xlsx file upload
>>> # where you will read from requests.FILES['YOUR_XLSX_FILE']
>>> xlsxfile = "another_file.xlsx"
>>> with open(xlsxfile, "rb") as f:
...     content = f.read()
...     r = pe.get_book(file_type="xlsx", file_content=content)
...     print(r)
...
Sheet 1:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
Sheet 2:
+-------+-------+-------+
| row 1 | row 2 | row 3 |
+-------+-------+-------+

写入 StringIO 实例

您需要将 StringIO 实例传递给 Writer:

>>> data = [
...     [1, 2, 3],
...     [4, 5, 6]
... ]
>>> io = StringIO()
>>> sheet = pe.Sheet(data)
>>> io = sheet.save_to_memory("xlsx", io)
>>> # then do something with io
>>> # In reality, you might give it to your http response
>>> # object for downloading

执照

新的 BSD 许可证

开发者指南

代码更改的开发步骤

  1. git 克隆https://github.com/pyexcel/pyexcel-xlsx.git

  2. cd pyexcel-xlsx

升级您的设置工具和点子。它们仅用于开发和测试:

  1. pip install --upgrade setuptools pip

然后安装相关的开发需求:

  1. pip install -r rnd_requirements.txt # 如果这样的文件存在

  2. pip install -r requirements.txt

  3. pip install -r 测试/requirements.txt

完成更改后,请提供测试用例、相关文档并更新 CHANGELOG.rst。

如何测试你的贡献

虽然在代码测试中都使用了nosedoctest ,但还是建议将单元测试放在测试中。doctest仅用于确保文档中的代码示例在不同的开发版本中保持有效。

在 Linux/Unix 系统上,请像这样启动您的测试:

$ make

在 Windows 系统上,请发出以下命令:

> test.bat

在你提交之前

请运行:

$ make格式

以便美化您的代码,否则 travis-ci 可能无法通过您的单元测试。

更改日志

0.6.0 - 10.10.2020

更新

  1. 由 pyexcel-io v0.6.2 推广的新型 xlsx 插件。

0.5.8 - 28.12.2019

更新

  1. #34 : pin openpyxl>=2.6.1

0.5.7 - 15.02.2019

添加

  1. pyexcel-io#66 pin openpyxl < 2.6.0

0.5.6 - 26.03.2018

添加

  1. #24,从 merge_cell_ranges 和 get_sheet_by_name 中删除已弃用的警告

0.5.5 - 18.12.2017

添加

  1. #22,检测 xlsx 中的合并单元格 - 快速跟踪的 patreon 请求。

0.5.4 - 2.11.2017

更新

  1. 对齐 skip_hidden_​​row_and_column 的行为。默认为真。

0.5.3 - 2.11.2017

添加

  1. #20,跳过“skip_hidden_​​row_and_column”标志下的隐藏行和列。

0.5.2 - 23.10.2017

更新

  1. pyexcel pyexcel#105,从 setup_requires 中删除 gease,由 0.5.1 引入。

  2. 移除python2.6测试支持

  3. 将其对 pyexcel-io 的依赖更新为 0.5.3

0.5.1 - 20.10.2017

添加

  1. pyexcel#103,在 MANIFEST.in 中包含 LICENSE 文件,这意味着 LICENSE 文件将出现在发布的 tar 球中。

0.5.0 - 30.08.2017

更新

  1. 依赖 pyexcel-io 0.5.0,它使用 cStringIO 而不是 StringIO。因此,在处理内存中的文件时会提高性能。

已移除

  1. #18,在 pyexcel-io 中处理

0.4.2 - 25.08.2017

更新

  1. #18、处理http响应给出的不可搜索的流

0.4.1 - 16.07.2017

已移除

  1. 删除了无用的代码

0.4.0 - 19.06.2017

更新

  1. #14、关闭文件句柄

  2. pyexcel-io 插件界面现在更新为使用lml

0.3.0 - 22.12.2016

更新

  1. 使用 pyexcel-io v 0.3.0 重构代码

  2. #13,打开 openpyxl 上的 read_only 标志。

0.2.3 - 05.11.2016

更新

  1. #12,删除 UserWarning:不推荐使用 ws.cell 的坐标。使用 ws[坐标]

0.2.2 - 31.08.2016

添加

  1. 支持分页。两对:start_row、row_limit 和 start_column、column_limit 帮助您处理大文件。

0.2.1 - 12.07.2016

添加

  1. #8, 添加了skip_hidden_​​sheets。默认情况下,阅读所有工作表时会跳过隐藏工作表。按名称或按索引阅读表不受影响。

0.2.0 - 01.06.2016

添加

  1. 添加了“library=pyexcel-xlsx”以通知 pyexcel 使用它而不是其他库,在一种文件类型有多个插件的情况下,例如 xlsm

更新

  1. 支持pyexcel-io 0.2.0的自动导入功能

0.1.0 - 17.01.2016

添加

  1. 将“streaming=True”传递给get_data,您将得到二维数组作为生成器

  2. 将“data=your_generator”传递给 save_data 也是可以接受的。

更新

  1. 与 pyexcel-io 0.1.0 的兼容性

项目详情