首页 > 代码库 > python 操作excel表格

python 操作excel表格

                       Python 操作excel 表格

#coding=utf-8

import xlsxwriter

#1.创建excel 对象

work = xlsxwriter.Workbook(‘hello.xlsk‘)#在当前目录下创建一个Excel文件

#2. 创建表格

worksheet = work.add_worksheet(‘int‘)

#3.写入内容

title_index = ["A","B","C","D","E","F"]

title = ["int","for","while","range","mk","二狗".decode("utf-8")]

data = http://www.mamicode.com/[18,19,20,21,17,26]

 

#创建一个格式

b = work.add_format({"blod":True})

for i,j in enumerate(title_index)

     point = "%s%s"%(j,1)

     content = title[i]

     worksheet.write(point,content,b)

for i,j in enumerate(title_index):

      point = "%s%s"%(j,2)

      content = data[i]

      worksheet.write(point,content) 

 #数据统计

worksheet.write("G2","=SUM(A2:F2)")

#把数据在Excel表中生成图表

chart = work.add_chart({"type":"column"})#创建一个柱状图

    #column 柱状图

    #area 面积图

     #bar 条形图

     # line 折线图

     # radar 雷达图

chart.add_series(

       {

          "categories":"=int!$A$1:$F$1",#类别标签的范围

            "values":"=int!$A$2:$F$2",#图标数据的范围

            "line":{"color":"red"}#图标线条的属性

          })

worksheet.insert_chart("A4",chart)

#关闭并且保存操作

work.close()

 

python 操作excel表格