首页 > 代码库 > python操作excel

python操作excel

from openpyxl import Workbook

from openpyxl import load_workbook

import xlrd

from xlrd import open_workbook,cellname

存储到excel文件

wb = Workbook()
ws = wb.active

ws.append(“表头”)
wb.save(‘filename.xlsx‘)

读取excel文件

data = http://www.mamicode.com/xlrd.open_workbook(‘filename.xlsx‘)

table = data.sheets()[0]    //通过索引得到表
nrows = table.nrows    //获得表的行数

ncols = table.ncols  //获得表的列数

循环行列表数据
 for i in range(nrows ):
      print table.row_values(i)
 
单元格
cell_A1 = table.cell(0,0).value
 
cell_C4 = table.cell(2,3).value
 
使用行列索引
cell_A1 = table.row(0)[0].value
 
cell_A2 = table.col(1)[0].value
 
简单的写入
row = 0
 
col = 0
 
# 类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
ctype = 1 value = http://www.mamicode.com/‘单元格的值‘
 
xf = 0 # 扩展的格式化
 
table.put_cell(row, col, ctype, value, xf)
 
table.cell(0,0)  #单元格的值
 
table.cell(0,0).value #单元格的值
 

python操作excel