自动化办公:python 操作Excel
1.安装
pip install xlsxwriter
or
easy_install xlsxwriter
or
tar -zxvf xlsxwriter-..*.tar.gz
python setup.py install
2.操作一个简单的Excel文档操作注释及代码:
引入依赖模块
import xlsxwriter
数据准备
datas = (
['Rent', 1000],
['Gas', 100 ],
['Food', 300 ],
['Gym', 50 ],
)创建一个Excel文档
workbook = xlsxwriter.Workbook('ex01.xlsx')
添加一个工作表
worksheet = workbook.add_worksheet()
设置行和列的偏移
row, col = 0, 0
开始添加数据
for item, cost in datas:
指定行、列的单元格,添加数据
worksheet.write(row, col, item)
worksheet.write(row, col+1, cost)行增加
row += 1
添加一个计算总数的函数
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)')关闭文档
workbook.close()
操作完成后,数据存储结果如下:自动化办公:python 操作Excel
3. 操作简单Excel文档并添加数据格式
操作代码如下:附带数据格式的定义
导入依赖的模块
import xlsxwriter
数据准备
datas = (
['Rent', 1000],
['Gas', 100 ],
['Food', 300 ],
['Gym', 50 ],
)创建表格
workbook = xlsxwriter.Workbook('ex02.xlsx')
worksheet = workbook.add_worksheet('data')添加全局格式
bold = workbook.add_format({'bold': True})
添加money格式
money = workbook.add_format({'num_format': '$#,##0'})
添加表格头,附带格式
worksheet.write('A1', 'Item', bold)
worksheet.write('B1', 'Cost', bold)数据表格偏移
row, col = 1, 0
添加数据
for item, cost in datas:
按照行列单元格添加数据
worksheet.write(row, col, item)
worksheet.write(row, col+1, cost)
row += 1添加一列求和计数
worksheet.write(row, 0, 'Total', bold)
worksheet.write(row, 1, '=SUM(B2:B5)', money)存储退出
workbook.close()
操作效果如图所示:自动化办公:python 操作Excel
4.Excel中添加不同类型的数据
操作代码如下:将不同的数据按照指定的格式添加到文件中
引入依赖的模块
from datetime import datetime
import xlsxwriter数据准备
datas = (
['Rent', '2018-05-13', 1000],
['Gas', '2018-05-14', 100],
['Food', '2018-05-16', 300],
['Gym', '2018-05-20', 50],
)创建表格
workbook = xlsxwriter.Workbook('ex03.xlsx')
worksheet = workbook.add_worksheet('data')添加格式
bold_f = workbook.add_format({'bold': True})
money_f = workbook.add_format({'num_format': '$#,##0'})添加日期格式new
date_f = workbook.add_format({'num_format': 'mmmm d yyyy'})
添加对齐方式和表格宽度new
worksheet.set_column(1, 1, 15)
添加表头数据
worksheet.write('A1', 'Item', bold_f)
worksheet.write('B1', 'Date', bold_f)
worksheet.write('C1', 'Cost', bold_f)定义偏移值
row, col = 1, 0
添加数据
for item, date, cost in datas:
date = datetime.strptime(date, '%Y-%m-%d')
worksheet.write(row, col, item)
worksheet.write_datetime(row, col+1, date, date_f)
worksheet.write(row, col+2, cost, money_f)
row += 1添加计数
worksheet.write(row, 0, 'Total', bold_f)
worksheet.write(row, 2, '=SUM(C2:C5)', money_f)存储并关闭
workbook.close()
代码执行结果如下:自动化办公:python 操作Excel
注意:xlsxwriter中提供了向excel中写入数据的多种方式,如下:
write_string()【https://xlsxwriter.readthedocs.io/worksheet.html#write_string】
write_number()【https://xlsxwriter.readthedocs.io/worksheet.html#write_number】
write_blank()【https://xlsxwriter.readthedocs.io/worksheet.html#write_blank】
write_formula()【https://xlsxwriter.readthedocs.io/worksheet.html#write_formula】
write_datetime()【https://xlsxwriter.readthedocs.io/worksheet.html#write_datetime】
write_boolean()【https://xlsxwriter.readthedocs.io/worksheet.html#write_boolean】
write_url()【https://xlsxwriter.readthedocs.io/worksheet.html#write_url】
5.Excel中添加数据图表操作代码如下:将对应的数据展示图表添加到文件中
引入依赖模块
import xlsxwriter
操作数据
data = [20, 45, 26, 18, 45]
创建表格
workbook = xlsxwriter.Workbook("ex04.xlsx")
worksheet = workbook.add_worksheet("data")添加数据:一次添加多个数据
worksheet.write_column('A1', data)
创建图表
chart = workbook.add_chart({'type': 'line'})
图表添加数据
chart.add_series({
'values': '=data!$A1:$A6',
'name': '图表线名称',
'marker': {
'type': 'circle',
'size': 8,
'border': {'color': 'black'},
'fill': {'color': 'red'}
},
'data_labels': {'value': True},
'trendline': {
'type': 'polynomial',
'order': 2,
'name': '示例趋势线',
'forward': 0.5,
'backward': 0.5,
'display_equation': True,
'line': {'color': 'red', 'width': 1, 'dash_type': 'long_dash'}
}
})
worksheet.insert_chart('C1', chart)
workbook.close()


https://blog.51cto.com/14186420/2364062