zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python编程-Office操作-操作Excel(上)

PythonExcel编程 操作 Office
2023-09-11 14:16:56 时间

首先,需要安装openpyxl库

http://openpyxl.readthedocs.org/en/default/

pyton 2.x
pip install openpyxl

 

python 3.x

easyinstall openpyxl

 

准备测试excel文件

 

firstExcel.py

 

import openpyxl

wb = openpyxl.load_workbook('example.xlsx')
print(wb.get_sheet_names())

sheet = wb.get_sheet_by_name('Sheet3')
print(sheet.title)

sheet = wb.get_sheet_by_name('Sheet1')
print(sheet['A1'].value)
c = sheet['B1']
print(c.value)
print('Row ' + str(c.row) + ', Column ' + c.column + ' is ' + c.value)
print('Cell ' + c.coordinate + ' is ' + c.value)
print(sheet['C1'].value)

 

运行结果:

['Sheet1', 'Sheet2', 'Sheet3']
Sheet3
2015-04-05 13:34:02
Apples
Row 1, Column B is Apples
Cell B1 is Apples
73