熱線電話:13121318867

登錄
首頁精彩閱讀python實現數據寫入excel表格
python實現數據寫入excel表格
2018-06-28
收藏

python實現數據寫入excel表格

這篇文章主要為大家詳細介紹了python實現數據寫入excel表格,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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()
以上就是本文的全部內容,希望對大家的學習有所幫助

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢