python读写excel

python操作excel需要安装openpyxl库,执行sudo pip install openpyxl即可。

基本概念

常用方法

示例

假设有以下数据表格要读取:

idnamedeviceip
101chenfy1linux133192.168.1.133
102chenfy2linux134192.168.1.134

import openpyxl

wb = openpyxl.load_workbook('test.xlsx')
sh = wb.get_sheet_names()
print sh
cur = wb.get_active_sheet()
print cur.title

for i in xrange(1, cur.max_row + 1):
    for j in xrange(1, cur.max_column + 1):
        print cur.cell(row=i, column=j).value,
    print

运行结果如下:

chenfy@study python $ ./b.py
[u'Sheet1', u'Sheet2', u'Sheet3']
Sheet1
id name device ip
101 chenfy1 linux133 192.168.1.133
102 chenfy2 linux134 192.168.1.134
Table of Contents