- #-*- coding:utf-8 -*
- from openpyxl.reader.excel import load_workbook
- import MySQLdb
- import time
- #开始时间
- startTime = time.time()
- #读取excel2007文件
- wb = load_workbook(filename = r'empty_book.xlsx' )
- #显示有多少张表
- print "Worksheet range(s):" , wb.get_named_ranges()
- print "Worksheet name(s):" , wb.get_sheet_names()
- #取第一张表
- sheetnames = wb.get_sheet_names()
- ws = wb.get_sheet_by_name(sheetnames[0 ])
- #显示表名,表行数,表列数
- print "Work Sheet Titile:" ,ws.title
- print "Work Sheet Rows:" ,ws.get_highest_row()
- print "Work Sheet Cols:" ,ws.get_highest_column()
- # 建立存储数据的字典
- data_dic = {}
- #把数据存到字典中
- for rx in range(ws.get_highest_row()):
- temp_list = []
- pid = ws.cell(row = rx,column = 0 ).value
- w1 = ws.cell(row = rx,column = 1 ).value
- w2 = ws.cell(row = rx,column = 2 ).value
- w3 = ws.cell(row = rx,column = 3 ).value
- w4 = ws.cell(row = rx,column = 4 ).value
- temp_list = [w1,w2,w3,w4]
- data_dic[pid] = temp_list
- #打印字典数据个数
- print 'Total:%d' %len(data_dic)
#-*- coding:utf-8 -* from openpyxl.reader.excel import load_workbook import MySQLdb import time #开始时间 startTime = time.time() #读取excel2007文件 wb = load_workbook(filename = r'empty_book.xlsx') #显示有多少张表 print "Worksheet range(s):", wb.get_named_ranges() print "Worksheet name(s):", wb.get_sheet_names() #取第一张表 sheetnames = wb.get_sheet_names() ws = wb.get_sheet_by_name(sheetnames[0]) #显示表名,表行数,表列数 print "Work Sheet Titile:",ws.title print "Work Sheet Rows:",ws.get_highest_row() print "Work Sheet Cols:",ws.get_highest_column() # 建立存储数据的字典 data_dic = {} #把数据存到字典中 for rx in range(ws.get_highest_row()): temp_list = [] pid = ws.cell(row = rx,column = 0).value w1 = ws.cell(row = rx,column = 1).value w2 = ws.cell(row = rx,column = 2).value w3 = ws.cell(row = rx,column = 3).value w4 = ws.cell(row = rx,column = 4).value temp_list = [w1,w2,w3,w4] data_dic[pid] = temp_list #打印字典数据个数 print 'Total:%d' %len(data_dic)
注意的是ws.cell()方法,支持的参数有两种,cell( coordinate=None , row=None , column=None)
coordinate坐标,eg ws.cell("B1")
row 和 column 是行和列 ,都是从0开始
还有,如果想取得格里的值,得用ws.cell("A1").value 取到,如果用过xlrd,因为写法差不多,可能就会忘记加value了。
III.写入excel2007
- 写excel2007
- #-*- coding:utf-8 -*
- import MySQLdb
- import time
- import sys
- #workbook相关
- from openpyxl.workbook import Workbook
- from openpyxl.writer.excel import ExcelWriter
- #一个eggache的数字转为列字母的方法
- from openpyxl.cell import get_column_letter
- #新建一个workbook
- wb = Workbook()
- #新建一个excelWriter
- ew = ExcelWriter(workbook = wb)
- #设置文件输出路径与名称
- dest_filename = r'empty_book.xlsx'
- #第一个sheet是ws
- ws = wb.worksheets[0 ]
- #设置ws的名称
- ws.title = "range names"
- #录入数据,注意col是数字转字母,然后需要限定%s(string型)当参数传到ws.cell()方法中去,records可以想象为一个从数据库里查询出来的数据集合
- i=1
- table = {}
- for record in records:
- for x in range( 1 ,len(record)+ 1 ):
- col = get_column_letter(x)
- ws.cell('%s%s' %(col, i)).value = '%s' % (record[x- 1 ])
- i+=1
- #又建了一个sheet,ws名字都没变,太省了。。。但是确实是一个新的sheet,不会影响之前那个sheet的东西
- ws = wb.create_sheet()
- ws.title = 'Pi'
- ws.cell('F5' ).value = 3.14
- #写文件
- ew.save(filename = dest_filename)
写excel2007 #-*- coding:utf-8 -* import MySQLdb import time import sys #workbook相关 from openpyxl.workbook import Workbook #万恶的ExcelWriter,妹的封装好了不早说,封装了很强大的excel写的功能 from openpyxl.writer.excel import ExcelWriter #一个eggache的数字转为列字母的方法 from openpyxl.cell import get_column_letter #新建一个workbook wb = Workbook() #新建一个excelWriter ew = ExcelWriter(workbook = wb) #设置文件输出路径与名称 dest_filename = r'empty_book.xlsx' #第一个sheet是ws ws = wb.worksheets[0] #设置ws的名称 ws.title = "range names" #录入数据,注意col是数字转字母,然后需要限定%s(string型)当参数传到ws.cell()方法中去,records可以想象为一个从数据库里查询出来的数据集合 i=1 table = {} for record in records: for x in range(1,len(record)+1): col = get_column_letter(x) ws.cell('%s%s'%(col, i)).value = '%s' % (record[x-1]) i+=1 #又建了一个sheet,ws名字都没变,太省了。。。但是确实是一个新的sheet,不会影响之前那个sheet的东西 ws = wb.create_sheet() ws.title = 'Pi' ws.cell('F5').value = 3.14 #写文件 ew.save(filename = dest_filename)
注意的地方:
# col是用列号x为参数,调用了这个模块的get_column_letter方法算出来的字母,这个比较蛋疼。
col = get_column_letter(x)
#在为数据格赋值的时候,注意写的格式:要不会有各种不靠谱的问题出现(这个是用坐标的方式写的,其实用row ,col的方式可能没那么麻烦)
ws.cell( '%s%s'%(col, i) ).value = '%s' % (record[x-1])