python+openpyxl操作excel

url='D:student.xlsx'#要操作的excel路径
wj=openpyxl.load_workbook(url)#打开excel
print wj.get_sheet_names()#获取所有sheet页名字
sheet1=wj.get_sheet_by_name('stu')#得到指定的sheet页
print sheet1.title
activsheet=wj.get_active_sheet()
print activsheet.title #获取活动的工作表

print sheet1['A1'].value#获得某个单元格的数据
print sheet1.cell(row=1,column=1).value#获得某个单元格的数据

#写入单元格数据
activsheet['B8'].value='test'
activsheet['A3'].value='losi'

rows=activsheet.max_row
columns=activsheet.max_column
print "最大行%d"%rows
print "最大列%d"%columns
activsheet.cell(row=4,column=5).value='test'
activsheet['B5'].value='yy'
activsheet.append([3,3,3,3])#最后一行追加
wj.save(url)#excel操作完之后要记得保存
#A1,B1, C1这样的顺序
for row in activsheet.rows:
for cell in row:
print(cell.value)
#A1, A2, A3这样的顺序
for column in activsheet.columns:
for cell in column:
print(cell.value)
#获取第三行的数据
for cell in list(activsheet.rows)[2]:
print(cell.value)
# 根据列的数字返回字母
print(get_column_letter(2))




原文地址:https://www.cnblogs.com/hhm8hhm/p/7401872.html