openpyxl

openpyxl库的使用,这个处理xlsx还是挺有用的

ref:传送门

 1 from openpyxl import Workbook
 2 from openpyxl import load_workbook
 3 from openpyxl.cell import get_column_letter
 4 
 5 #---------------读取表内容
 6 wb=load_workbook(filename=r'1.xlsx');   #打开一个xlsx文件
 7 sheetnames = wb.get_sheet_names()       # get a list of all the sheet names in the workbook by calling the get_sheet_names() method
 8 ws = wb.get_sheet_by_name(sheetnames[0])    #获取第一个表
 9 for i in range(1,3):                    #获取表内容  或者ws['B2'].value
10     for j in range(1,4):
11         print ws.cell(row=i,column=j).value,
12     print
13 print ws.max_column,ws.max_row;     #获取行,列数
14 print get_column_letter(6);
15 for i in ws['A1':'C2']: #获取每一行
16     #print i
17     for j in i:
18         print j.coordinate,j.value
19 
20 #-------------创建表写入内容
21 a=Workbook()
22 a.create_sheet(index=0,title='66')  #表名
23 sheet=a.get_sheet_by_name('66');
24 for i in range(1,5):
25     for j in range(1,2):
26         sheet.cell(row=i,column=j).value='7'
27 '''
28 for i in sheet['A1':'C5']:
29     for j in i:
30         j.value='6';
31 '''
32 a.save('6.xlsx');

 

posted @ 2016-07-25 16:41  御心飞行  阅读(557)  评论(0编辑  收藏  举报