python实现数据写入excel表格
作者:lingluofengzang 时间:2023-07-04 00:39:41
本文实例为大家分享了python数据写入excel表格的具体代码,供大家参考,具体内容如下
安装: xlsxwriter第三方库
code:
#!/usr/bin/env/python
#_*_coding:utf-8_*_
#Data:2017-08-13
#Auther:苏莫
#Link:http://blog.csdn.net/lingluofengzang
#PythonVersion:python2.7
#filename:xlsx.py
import sys
# import os
import xlsxwriter
'''
pip install xlsxwriter
'''
reload(sys)
sys.setdefaultencoding("utf-8")
# path = os.path.dirname(os.path.abspath(__file__))
# 建立文件
workbook = xlsxwriter.Workbook("text.xlsx")
# 可以制定表的名字
# worksheet = workbook.add_worksheet('text')
worksheet = workbook.add_worksheet()
# 设置列宽
# worksheet.set_column('A:A',10)
# 设置祖体
bold = workbook.add_format({'bold':True})
# 定义数字格式
# money = workbook.add_format({'num_format':'$#,##0'})
# 写入带粗体的数据
worksheet.write('A1','data',bold)
worksheet.write('B1','work')
'''
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()
'''
worksheet.write('A3',15)
worksheet.write('B3',20)
worksheet.write('C3',44)
worksheet.write('D3',36)
# xlsx计算数据
worksheet.write('E3','=SUM(A3:D3)')
'''
建立Chart对象: chart = workbook.add_chart({type, 'column'})
Chart: Area, Bar, Column, Doughnut, Line, Pie, Scatter, Stock, Radar
将图插入到sheet中: worksheet.insert_chart('A7', chart)
'''
# 定义插入的图标样式
chart = workbook.add_chart({"type":'column'})
headings = ['a','b','c']
data = [
[1,2,3,4,5],
[2,4,6,8,10],
[3,6,9,12,15],
]
# 按行插入数据
worksheet.write_row('A4',headings)
# 按列插入数据
worksheet.write_column('A5',data[0])
worksheet.write_column('B5',data[1])
worksheet.write_column('C5',data[2])
# 图行的数据区
# name:代表图例名称;
# categories:是x轴项,也就是类别;
# values:是y轴项,也就是值;
chart.add_series({
'name':'=Sheet1!$B$4',
'categories':'=Sheet1!$A$5:$A$9',
'values':'=Sheet1!$B$5:$B$9',
})
chart.add_series({
'name':['Sheet1', 3, 2],
'categories':['Sheet1', 4, 0, 8, 0],
'values':['Sheet1', 4, 2, 8, 2],
})
# 图形的标题
chart.set_title ({'name': 'Percent Stacked Chart'})
# 图形X轴的说明
chart.set_x_axis({'name': 'Test number'})
# 图形Y轴的说明
chart.set_y_axis({'name': 'Sample length (mm)'})
# 设置图表风格
chart.set_style(11)
# 插入图形,带偏移
worksheet.insert_chart('D12',chart,{'x_offset': 25, 'y_offset': 10})
workbook.close()
来源:https://blog.csdn.net/lingluofengzang/article/details/78209237
标签:python,excel,表格
0
投稿
猜你喜欢
php中mysql连接方式PDO使用详解
2023-11-06 02:46:43
Python实现AI自动抠图实例解析
2021-12-25 11:04:22
微信小程序 scroll-view实现上拉加载与下拉刷新的实例
2024-04-23 09:30:40
如何利用python将Xmind用例转为Excel用例
2022-06-18 19:18:46
深入浅析Pytorch中stack()方法
2021-12-26 01:40:08
Go之interface的具体使用
2024-02-08 18:58:41
精心整理总结的Python自动化测试面试题
2022-07-27 08:13:00
Windows下使Python2.x版本的解释器与3.x共存的方法
2021-03-14 22:22:17
用python打开摄像头并把图像传回qq邮箱(Pyinstaller打包)
2021-04-26 22:34:55
解决Pycharm下面出现No R interpreter defined的问题
2021-03-03 01:33:28
Python实现处理管道的方法
2022-05-17 22:58:29
Python之捕捉异常详解
2022-06-10 02:27:30
python实现K折交叉验证
2023-06-08 18:49:01
Zend Framework动作助手Redirector用法实例详解
2024-05-13 09:53:15
django session完成状态保持的方法
2021-12-07 16:12:02
Python对数据进行插值和下采样的方法
2022-06-02 03:36:41
常用的javascript设计模式
2023-08-18 06:54:27
关于php开启错误提示的总结
2023-11-04 10:46:21
python翻译软件实现代码(使用google api完成)
2023-05-19 17:25:43
Dojo Style Javascript 编程规范
2007-10-25 17:24:00