zoukankan      html  css  js  c++  java
  • python操作Excel的几种方式

      1 Python对Excel的读写主要有xlrd、xlwt、xlutils、openpyxl、xlsxwriter几种。
      2 
      3 1.xlrd主要是用来读取excel文件
      4 
      5 import xlrd
      6 
      7 workbook = xlrd.open_workbook(u'有趣装逼每日数据及趋势.xls')
      8 
      9 sheet_names= workbook.sheet_names()
     10 
     11 for sheet_name in sheet_names:
     12 
     13    sheet2 = workbook.sheet_by_name(sheet_name)
     14 
     15    print sheet_name rows = sheet2.row_values(3) # 获取第四行内容
     16 
     17    cols = sheet2.col_values(1) # 获取第二列内容
     18 
     19    print rows
     20 
     21    print cols
     22 
     23  
     24 
     25 2.xlwt主要是用来写excel文件
     26 
     27 import xlwt
     28 
     29 wbk = xlwt.Workbook()
     30 
     31 sheet = wbk.add_sheet('sheet 1')
     32 
     33 sheet.write(0,1,'test text')#第0行第一列写入内容
     34 
     35 wbk.save('test.xls')
     36 
     37  
     38 
     39 3.xlutils结合xlrd可以达到修改excel文件目的
     40 
     41 import xlrd
     42 
     43 from xlutils.copy import copy
     44 
     45 workbook = xlrd.open_workbook(u'有趣装逼每日数据及趋势.xls')
     46 
     47 workbooknew = copy(workbook)
     48 
     49 ws = workbooknew.get_sheet(0)
     50 
     51 ws.write(3, 0, 'changed!')
     52 
     53 workbooknew.save(u'有趣装逼每日数据及趋势copy.xls')
     54 
     55  
     56 
     57 4.openpyxl可以对excel文件进行读写操作
     58 
     59 from openpyxl import Workbook
     60 
     61 from openpyxl import load_workbook
     62 
     63 from openpyxl.writer.excel import ExcelWriter 
     64 
     65  
     66 
     67 workbook_ = load_workbook(u"新歌检索失败1477881109469.xlsx")
     68 
     69 sheetnames =workbook_.get_sheet_names() #获得表单名字
     70 
     71 print sheetnames
     72 
     73 sheet = workbook_.get_sheet_by_name(sheetnames[0])
     74 
     75 print sheet.cell(row=3,column=3).value
     76 
     77 sheet['A1'] = '47' 
     78 
     79 workbook_.save(u"新歌检索失败1477881109469_new.xlsx")  
     80 
     81 wb = Workbook()
     82 
     83 ws = wb.active
     84 
     85 ws['A1'] = 4
     86 
     87 wb.save("新歌检索失败.xlsx") 
     88 
     89      
     90 
     91 5.xlsxwriter可以写excel文件并加上图表
     92 
     93 import xlsxwriter
     94 
     95  
     96 
     97 def get_chart(series):
     98 
     99     chart = workbook.add_chart({'type': 'line'})
    100 
    101     for ses in series:
    102 
    103         name = ses["name"]
    104 
    105         values = ses["values"]
    106 
    107         chart.add_series({ 
    108 
    109             'name': name,
    110 
    111             'categories': 'A2:A10',
    112 
    113             'values':values
    114 
    115         })  
    116 
    117     chart.set_size({'width': 700, 'height': 350}) 
    118 
    119     return chart
    120 
    121  
    122 
    123 if __name__ == '__main__':
    124 
    125     workbook = xlsxwriter.Workbook(u'H5应用中心关键数据及趋势.xlsx') 
    126 
    127     worksheet = workbook.add_worksheet(u"每日PV,UV")
    128 
    129     headings = ['日期', '平均值']
    130 
    131     worksheet.write_row('A1', headings)
    132 
    133     index=0
    134 
    135     for row in range(1,10):
    136 
    137         for com in [0,1]:
    138 
    139             worksheet.write(row,com,index)
    140 
    141             index+=1  
    142 
    143     series = [{"name":"平均值","values":"B2:B10"}]
    144 
    145     chart = get_chart(series)
    146 
    147     chart.set_title ({'name': '每日页面分享数据'})  
    148 
    149     worksheet.insert_chart('H7', chart)
    150 
    151     workbook.close()
  • 相关阅读:
    Win32汇编
    Boost ASIO 实现异步IO远控
    Python 使用oslo.vmware管理ESXI虚拟机
    Python 巡检接入钉钉机器人
    Django Ajax序列化与反序列化
    Nacos 认证绕过
    Lanproxy 遍历目录漏洞 CVE-2021-3019 附批量POC
    Apache Solr 全版本任意读取文件漏洞
    垂直水平居中的多种方法 主要的4种
    vue provide/inject 父组件如何给孙子组件传值
  • 原文地址:https://www.cnblogs.com/hushaojun/p/7792545.html
Copyright © 2011-2022 走看看