使用xlsxwriter保存pyexcel数据,同时保留良好的格式。
pyexcel-xlsxwx的Python项目详细描述
PYEXCEL xlsxwx
使用XlsxWriter保存pyexcel数据,同时保留良好的格式
功能
- 允许设置列宽和自动换行
- 不包括用于读取数据的包,请参见^{
}'s plugins here 。
安装
$ pip install pyexcel-xlsxwx
注意pyexcel
不是依赖项。
用法
>>>importpyexcel_xlsxwx>>>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"]]})>>>pyexcel_xlsxwx.save_data("your_file.xlsx",data)
您还可以通过以下方式定义自定义配置:
>>>pyexcel_xlsxwx.save_data("your_file.xlsx",data,config=config)
其中config可以是字典或yaml文件的路径。
默认的YAML配置为:
workbook:constant_memory:truestrings_to_numbers:falsestrings_to_formulas:falsestrings_to_urls:trueworksheet:_default:freeze_panes:A2# column_width: 30smart_fit:truemax_column_width:30format:_default:valign:toptext_wrap:true
column_width
还可以接受一个列表和一个字典,其中key表示列。
row_height
也可以用同样的方式设置。
要取消freeze_panes
,请尝试:
>>>pyexcel_xlsxwx.save_data("your_file.xlsx",data,config={'worksheet':{'_default':{'freeze_panes':None}}})
这些设置将合并(感谢https://stackoverflow.com/questions/20656135/python-deep-merge-dictionary-data),这样其他格式就不会丢失。
相关项目
- pyexcel-openpyxlx-导出xlsxwriter的样式。
- pyexcel-export-使用OpenPyXL操作,它似乎有不好的自动换行支持但是,可以很好地保留格式。