python模块之XlsxWriter
官网Tutorial:http://xlsxwriter.readthedocs.io/tutorial
Xlsx是python用来构造xlsx文件的模块,可以向excel2007+中写text,numbers,formulas 公式以及hyperlinks超链接。
可以完成xlsx文件的自动化构造,包括:
合并单元格,制作excel图表等功能:
1,Introduction:
xlsxWriter支持多种excle功能;与excel完美兼容;写大文件,速度快且只占用很小的内存空间
不支持读或者改现有的excel文件
2, Installing:
sudo pip install XlsxWriter;
sudo easy_install XlsxWriter;
或者源码安装:http://github.com/jmcnamara/XlsxWriter/archive/master.tar.gz
3,使用:
import xlsxwriter workbook = xlsxwriter.Workbook('hello.xlsx') # 建立文件 worksheet = workbook.add_worksheet() # 建立sheet, 可以work.add_worksheet('employee')来指定sheet名,但中文名会报UnicodeDecodeErro的错误 worksheet.write('A1', 'Hello world') # 向A1写入 workbook.close()
excel公式计算
# Some data we want to write to the worksheet.
expenses = (
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
) # Start from the first cell. Rows and columns are zero indexed. 按标号写入是从0开始的,按绝对位置'A1'写入是从1开始的
row = 0
col = 0 # Iterate over the data and write it out row by row.
for item, cost in (expenses):
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1 # Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)') # 调用excel的公式表达式 workbook.close()
excel自定义格式:
import xlsxwriter # 建文件及sheet.
workbook = xlsxwriter.Workbook('Expenses02.xlsx')
worksheet = workbook.add_worksheet() # Add a bold format to use to highlight cells. 设置粗体,默认是False
bold = workbook.add_format({'bold': True}) # Add a number format for cells with money. 定义数字格式
money = workbook.add_format({'num_format': '$#,##0'}) # Write some data headers. 带自定义粗体blod格式写表头
worksheet.write('A1', 'Item', bold)
worksheet.write('B1', 'Cost', bold) # Some data we want to write to the worksheet.
expenses = (
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
) # Start from the first cell below the headers.
row = 1
col = 0 # Iterate over the data and write it out row by row.
for item, cost in (expenses):
worksheet.write(row, col, item) # 带默认格式写入
worksheet.write(row, col + 1, cost, money) # 带自定义money格式写入
row += 1 # Write a total using a formula.
worksheet.write(row, 0, 'Total', bold)
worksheet.write(row, 1, '=SUM(B2:B5)', money) workbook.close()
excel写入时间格式
from datetime import datetime
import xlsxwriter # Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Expenses03.xlsx')
worksheet = workbook.add_worksheet() # Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': 1}) # Add a number format for cells with money.
money_format = workbook.add_format({'num_format': '$#,##0'}) # Add an Excel date format.
date_format = workbook.add_format({'num_format': 'mmmm d yyyy'}) # Adjust the column width.
worksheet.set_column(1, 1, 15) # Write some data headers.
worksheet.write('A1', 'Item', bold)
worksheet.write('B1', 'Date', bold)
worksheet.write('C1', 'Cost', bold) # Some data we want to write to the worksheet.
expenses = (
['Rent', '2013-01-13', 1000],
['Gas', '2013-01-14', 100],
['Food', '2013-01-16', 300],
['Gym', '2013-01-20', 50],
) # Start from the first cell below the headers.
row = 1
col = 0 for item, date_str, cost in (expenses):
# Convert the date string into a datetime object.
date = datetime.strptime(date_str, "%Y-%m-%d") worksheet.write_string (row, col, item )
worksheet.write_datetime(row, col + 1, date, date_format )
worksheet.write_number (row, col + 2, cost, money_format)
row += 1 # Write a total using a formula.
worksheet.write(row, 0, 'Total', bold)
worksheet.write(row, 2, '=SUM(C2:C5)', money_format) workbook.close()
@@@ write方法提供了python类型到excel类型的转换, XlsxWriter支持excel工作表最大1048576行记录,16384条列记录,超出可以选择再建新sheet
worksheet.write(0, 0, 'Hello') # write_string()
worksheet.write(1, 0, 'World') # write_string()
worksheet.write(2, 0, 2) # write_number()
worksheet.write(3, 0, 3.00001) # write_number()
worksheet.write(4, 0, '=SIN(PI()/4)') # write_formula()
worksheet.write(5, 0, '') # write_blank()
worksheet.write(6, 0, None) # write_blank()
关于更多字符串、数字、颜色及位置等excel格式:http://xlsxwriter.readthedocs.io/format.html
4, 图标
这个是我比较关注的利用excel工具进行图标统计的功能
相比较python的matplotlib的画图模块,excel的图标更加漂亮灵活一些
Chart: Area, Bar, Column, Doughnut, Line, Pie, Scatter, Stock, Radar
workbook = xlswriter.Workbook('chart.xls')
worksheet = workbook.add_sheet('First_example') # 普通工作表
建立Chart对象: chart = workbook.add_chart({type, 'column'})
将图插入到sheet中: worksheet.insert_chart('A7', chart)
或者可以建立图表工作表chartsheet
chartsheet = workbook.add_charsheet()
chartsheet.set_char(chart)
柱状图:
import xlsxwriter workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet() # Create a new Chart object.
chart = workbook.add_chart({'type': 'column'}) # Write some data to add to plot on the chart.
data = [
[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
[3, 6, 9, 12, 15],
] worksheet.write_column('A1', data[0]) # 按列插入
worksheet.write_column('B1', data[1])
worksheet.write_column('C1', data[2]) # Configure the chart. In simplest case we add one or more data series.
chart.add_series({'values': '=Sheet1!$A$1:$A$5'})
chart.add_series({'values': '=Sheet1!$B$1:$B$5'})
chart.add_series({'values': '=Sheet1!$C$1:$C$5'}) # Insert the chart into the worksheet.
worksheet.insert_chart('A7', chart) workbook.close()
workbook.add_chart({'type':'column'}) # 默认格式
workbook.add_chart({'type':'column', 'substyle':'percent_stacked'}) # 按百分比展示
workbook.add_chart({'type':'column', 'substyle':'stacked'})
其他类型chart也是这样:
python模块之XlsxWriter的更多相关文章
- python模块:xlsxwriter和xlrd相结合读取、写入excel文件
python模块简单说明: xlsxwriter:负责写入数据 xlrd:负责读取数据 xlsxwriter 官方文档:http://xlsxwriter.readthedocs.org 本实例是刚写 ...
- python模块:xlsxwriter和xlrd相结合读取
python模块简单说明: xlsxwriter:负责写入数据 xlrd:负责读取数据 xlsxwriter 官方文档:http://xlsxwriter.readthedocs.org 本实例是刚写 ...
- python模块之XlsxWriter 详解
Xlsx是python用来构造xlsx文件的模块,可以向excel2007+中写text,numbers,formulas 公式以及hyperlinks超链接. 可以完成xlsx文件的自动化构造,包括 ...
- python 导出数据到excel 中,一个好用的导出数据到excel模块,XlsxWriter
最近公司有项目需要导出数据到excel,首先想到了,tablib,xlwt,xlrd,xlwings,win32com[还可以操作word],openpyxl,等模块但是 实际操作中tablib 写入 ...
- python模块大全
python模块大全2018年01月25日 13:38:55 mcj1314bb 阅读数:3049 pymatgen multidict yarl regex gvar tifffile jupyte ...
- [转]Python 模块收集
Python 模块收集 转自:http://kuanghy.github.io/2017/04/04/python-modules Python | Apr 4, 2017 | python 工具 a ...
- 使用C/C++写Python模块
最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...
- Python模块之configpraser
Python模块之configpraser 一. configpraser简介 用于处理特定格式的文件,其本质还是利用open来操作文件. 配置文件的格式: 使用"[]"内包含 ...
- Python模块之"prettytable"
Python模块之"prettytable" 摘要: Python通过prettytable模块可以将输出内容如表格方式整齐的输出.(对于用Python操作数据库会经常用到) 1. ...
随机推荐
- Delphi xe7组件和控件的安装方法
暂时我所遇到的所有控件安装方法大体与下面两种相同. 若有不同大家提出来,一起想办法解决. .dproj格式的组件安装方法: raise组件 安装详细步骤如下: 一.设置搜索路径1. 将本包中的文件连同 ...
- SQLAlchemy技术文档(中文版)(上)
在学习SQLAlchemy的过程中,好多时候需要查官方Tutorial,发现网上并没有完整的中文版,于是利用这两天空余时间粗略翻译了一下. 翻译效果很差....但也算是强迫自己通读一遍Tutorial ...
- [Leetcode] Interger to roman 整数转成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [Leetcode] subsets 求数组所有的子集
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- BZOJ2257 [Jsoi2009]瓶子和燃料 【裴蜀定理】
题目链接 BZOJ2257 题解 由裴蜀定理我们知道,若干的瓶子如此倾倒最小能凑出的是其\(gcd\) 现在我们需要求出\(n\)个瓶子中选出\(K\)个使\(gcd\)最大 每个数求出因数排序即可 ...
- bzoj 2566 calc 拉格朗日插值
calc Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 377 Solved: 226[Submit][Status][Discuss] Descr ...
- java 多线程 原子性
原子性 原子性:原子操作是不能被线程调度机制中断的操作,一旦操作开始,那么它就一定可以在可能发生的“上下文切换”之前(切换到其他线程执行)执行完毕. 依赖原子性是很棘手且很危险的,除非你是并发专家,否 ...
- mysql5.7.11安装遇到的问题
首次安装mysql5.7.11绿色版,真是破费功夫,现记录安装中遇到的问题,只是解决了问题,而不清楚问题的由来. 问题一: 问题二: 问题三: 问题四: 我的my.ini配置文件: [mysql] # ...
- 【Atcoder】ARC082 E - ConvexScore
[算法]计算几何 [题意]给定平面直角坐标系上的若干个点,任意选点连成凸多边形,凸多边形的价值定义为2^(n-|S|),其中n为凸多边形内部点数(含边界),|S|为顶点数,求总价值.n<=10^ ...
- DotNETCore 学习笔记 日志
Logging --------------------------------------------------------------------------------------- Impl ...