PyExcel的插件,提供以文本格式显示和写入数据的功能
pyexcel-text的Python项目详细描述
它是pyexcel的插件并扩展
它主要通过tabulate
:
- “普通”
- “简单”
- “网格”
- “管道”
- “orgtbl”
- “RST”
- “mediawiki”
- “乳胶漆”
- “乳胶书架”
- “json”
- “HTML”
从v0.2.7开始,也支持json
和ndjson
输入。
用法
有什么新消息?
>>>importpyexcelaspe>>>sheet=pe.Sheet()>>>sheet.json='[[1,2],[2,3]]'>>>sheetpyexcelsheet:+---+---+|1|2|+---+---+|2|3|+---+---+>>>highspeedrail=pe.Sheet()>>>highspeedrail.json=""" ... [{"year": 1903, "country": "Germany", "speed": "206.7km/h"}, ... {"year": 1964, "country": "Japan", "speed": "210km/h"}, ... {"year": 2008, "country": "China", "speed": "350km/h"}] ... """>>>highspeedrail.name='High Speed Train Speed Break Through (Source: Wikipedia)'>>>highspeedrailHighSpeedTrainSpeedBreakThrough(Source:Wikipedia):+---------+-----------+------+|country|speed|year|+---------+-----------+------+|Germany|206.7km/h|1903|+---------+-----------+------+|Japan|210km/h|1964|+---------+-----------+------+|China|350km/h|2008|+---------+-----------+------+>>>henley_on_thames_facts=pe.Sheet()>>>henley_on_thames_facts.json=""" ... {"area": "5.58 square meters", ... "population": "11,619", ... "civial parish": "Henley-on-Thames", ... "latitude": "51.536", ... "longitude": "-0.898" ... }""">>>henley_on_thames_factspyexcelsheet:+--------------------+------------------+----------+-----------+------------+|area|civialparish|latitude|longitude|population|+--------------------+------------------+----------+-----------+------------+|5.58squaremeters|Henley-on-Thames|51.536|-0.898|11,619|+--------------------+------------------+----------+-----------+------------+>>>ccs_insight=pe.Sheet()>>>ccs_insight.name="Worldwide Mobile Phone Shipments (Billions), 2017-2021">>>ccs_insight.json=""" ... {"year": ["2017", "2018", "2019", "2020", "2021"], ... "smart phones": [1.53, 1.64, 1.74, 1.82, 1.90], ... "feature phones": [0.46, 0.38, 0.30, 0.23, 0.17]}""">>>ccs_insightpyexcelsheet:+----------------+--------------+------+|featurephones|smartphones|year|+----------------+--------------+------+|0.46|1.53|2017|+----------------+--------------+------+|0.38|1.64|2018|+----------------+--------------+------+|0.3|1.74|2019|+----------------+--------------+------+|0.23|1.82|2020|+----------------+--------------+------+|0.17|1.9|2021|+----------------+--------------+------+
以下是json的变体:
>>> highspeedrail2 = pe.Sheet() >>> highspeedrail2.ndjson = """ ... {"year": 1903, "country": "Germany", "speed": "206.7km/h"} ... {"year": 1964, "country": "Japan", "speed": "210km/h"} ... {"year": 2008, "country": "China", "speed": "350km/h"} ... """.strip() >>> highspeedrail2.name = 'High Speed Train Speed Break Through (Source: Wikipedia)' >>> highspeedrail2 High Speed Train Speed Break Through (Source: Wikipedia): +---------+-----------+------+ | country | speed | year | +---------+-----------+------+ | Germany | 206.7km/h | 1903 | +---------+-----------+------+ | Japan | 210km/h | 1964 | +---------+-----------+------+ | China | 350km/h | 2008 | +---------+-----------+------+ >>> henley_on_thames_facts2 = pe.Sheet() >>> henley_on_thames_facts2.ndjson = """ ... {"area": "5.58 square meters"} ... {"population": "11,619"} ... {"civial parish": "Henley-on-Thames"} ... {"latitude": "51.536"} ... {"longitude": "-0.898"} ... """.strip() >>> henley_on_thames_facts2 pyexcel sheet: +---------------+--------------------+ | area | 5.58 square meters | +---------------+--------------------+ | population | 11,619 | +---------------+--------------------+ | civial parish | Henley-on-Thames | +---------------+--------------------+ | latitude | 51.536 | +---------------+--------------------+ | longitude | -0.898 | +---------------+--------------------+ >>> ccs_insight2 = pe.Sheet() >>> ccs_insight2.name = "Worldwide Mobile Phone Shipments (Billions), 2017-2021" >>> ccs_insight2.ndjson = """ ... {"year": ["2017", "2018", "2019", "2020", "2021"]} ... {"smart phones": [1.53, 1.64, 1.74, 1.82, 1.90]} ... {"feature phones": [0.46, 0.38, 0.30, 0.23, 0.17]} ... """.strip() >>> ccs_insight2 pyexcel sheet: +----------------+------+------+------+------+------+ | year | 2017 | 2018 | 2019 | 2020 | 2021 | +----------------+------+------+------+------+------+ | smart phones | 1.53 | 1.64 | 1.74 | 1.82 | 1.9 | +----------------+------+------+------+------+------+ | feature phones | 0.46 | 0.38 | 0.3 | 0.23 | 0.17 | +----------------+------+------+------+------+------+
简单
>>>importpyexcelaspe>>>content=[...["Column 1","Column 2","Column 3"],...[1,2,3],...[4,5,6],...[7,8,9]...]>>>sheet=pe.Sheet(content)>>>print(sheet.simple)pyexcelsheet:------------------------Column1Column2Column3123456789------------------------>>>sheet.name_columns_by_row(0)>>>print(sheet.simple)pyexcelsheet:Column1Column2Column3------------------------------123456789
网格
>>>print(sheet.grid)pyexcelsheet:+------------+------------+------------+|Column1|Column2|Column3|+============+============+============+|1|2|3|+------------+------------+------------+|4|5|6|+------------+------------+------------+|7|8|9|+------------+------------+------------+
mediawiki
>>>multiple_sheets={...'Sheet 1':...[...[1.0,2.0,3.0],...[4.0,5.0,6.0],...[7.0,8.0,9.0]...],...'Sheet 2':...[...['X','Y','Z'],...[1.0,2.0,3.0],...[4.0,5.0,6.0]...],...'Sheet 3':...[...['O','P','Q'],...[3.0,2.0,1.0],...[4.0,3.0,2.0]...]...}>>>book=pe.Book(multiple_sheets)>>>book.save_as("myfile.mediawiki")>>>myfile=open("myfile.mediawiki")>>>print(myfile.read())Sheet1:{|class="wikitable"style="text-align: left;"|+<!--caption-->|-|align="right"|1||align="right"|2||align="right"|3|-|align="right"|4||align="right"|5||align="right"|6|-|align="right"|7||align="right"|8||align="right"|9|}Sheet2:{|class="wikitable"style="text-align: left;"|+<!--caption-->|-|X||Y||Z|-|1.0||2.0||3.0|-|4.0||5.0||6.0|}Sheet3:{|class="wikitable"style="text-align: left;"|+<!--caption-->|-|O||P||Q|-|3.0||2.0||1.0|-|4.0||3.0||2.0|}>>>myfile.close()
html
>>>book.save_as("myfile.html")>>>myfile=open("myfile.html")>>>print(myfile.read())# doctest: +SKIPSheet1:<table><tr><tdstyle="text-align: right;">1</td><tdstyle="text-align: right;">2</td><tdstyle="text-align: right;">3</td></tr><tr><tdstyle="text-align: right;">4</td><tdstyle="text-align: right;">5</td><tdstyle="text-align: right;">6</td></tr><tr><tdstyle="text-align: right;">7</td><tdstyle="text-align: right;">8</td><tdstyle="text-align: right;">9</td></tr></table>Sheet2:<table><tr><td>X</td><td>Y</td><td>Z</td></tr><tr><td>1.0</td><td>2.0</td><td>3.0</td></tr><tr><td>4.0</td><td>5.0</td><td>6.0</td></tr></table>Sheet3:<table><tr><td>O</td><td>P</td><td>Q</td></tr><tr><td>3.0</td><td>2.0</td><td>1.0</td></tr><tr><td>4.0</td><td>3.0</td><td>2.0</td></tr></table>
请注意,表0.7.7在tr标签周围附加了一个tbody标签。
依赖关系
- 制表
更改日志
0.2.7-30.07.2017
已更新:
- 从纯json格式读取特定的数据结构:二维 数组,列表或二维列表和记录列表的字典。
- 读取换行分隔的json。二维数组 或者支持二维列表和记录列表。
0.2.6-19.06.2017
已更新:
- 支持PyExcel v0.5.0。插件接口更新为renderer
- 已删除不推荐的“另存为”和“另存为”内存函数
0.2.5-28.10.2016
已更新:
- 支持PyExcel v0.4.0
0.2.4-28.10.2016
添加:
- 支持PyExcel v0.3.0
0.2.3-14.07.2016
添加:
- json格式:序列化日期和日期时间
已更新:
- 如果工作表有行名,其json输出将成为记录(字典列表) 而不是行名与其余行值的字典。
0.2.2-01.06.2016
- 快速修复错误,请参见issue #27
0.2.1-01.06.2016
- PyExcel IO 0.2.0和PyExcel 0.2.2的紧凑性
0.2.0-23.04.2016
这是对整个扩展的完全重写。
已添加
- HTML支持
- 支持PyExcel 0.2.0的生成器输出
- 测试目标中的pypy和pypy3
- 支持文件流和点符号,例如pyexcel.sheet.rst将返回它的rst文本表示。
已更新
- #8,将头作为选项写入(false)以禁用头写入
- 多页书的json输出将按其页名排序。
- pyexcel文本不再是pyexcel io插件,而是pyexcel.sources插件。
0.1.1-30.01.2016
已更新
- #2,修复setup.py中的错误
0.1.0-17.01.2016
已更新
- 支持PyExcel 0.2.0
0.0.3-12.06.2015
已更新
- #1,对齐api接口
- 使用其他PyExcel插件,例如“另存为”、“另存书本为”
- #1,对齐api接口
- 使用其他PyExcel插件,例如“另存为”、“另存书本为”
0.0.2-30.11.2014
已更新
- 支持PyExcel 0.0.9
2014年11月20日
初始版本