python读写excel
主要是涉及到2个库,一个是xlrd,另一个是xlwt。
下载下来之后,不用安装,直接解压得到xlrd-0.9.4.tar\dist\xlrd-0.9.4
把那个文件夹拷贝到python的Lib\site-packages里去就好了。xlwt同理。
写excel表
一个最简单的demo
import xlwt
workbook = xlwt.Workbook(encoding = 'ascii') #这是设置编码模式
worksheet = workbook.add_sheet('My Worksheet') #创建sheet及命名
worksheet.write(0, 0, label = 'Row 0, Column 0 Value') 往单元格写入内容
workbook.save('Excel_Workbook.xls') #保存xls,而且只能保存为xls格式,xlsx都不行
这里需要注意,如果0行0列存在值,这样写会出错。修正办法:
table = file.add_sheet('sheet name',cell_overwrite_ok=True)
设置单元格可以覆盖写操作。
此外,还能设置style:
style = xlwt.XFStyle() #初始化样式
font = xlwt.Font() #为样式创建字体
font.name = 'Times New Roman'
font.bold = True
style.font = font #为样式设置字体
table.write(0, 0, 'some bold Times text', style) # 使用样式
读excel表
一个最简单的demo
import xlrd
data = xlrd.open_workbook('excel.xls') #这是设置编码模式
table = data.sheets()[0] #通过索引顺序获取
#table = data.sheet_by_index(0) #通过索引顺序获取
#table = data.sheet_by_name(u'Sheet1')#通过名称获取
#table.row_values(i) #返回整行值,数组形式返回
#table.col_values(i) #返回整列值,数组形式返回
#table.nrows #获取行数
#table.ncols #获取行数
#取某个单元格的值
print table.cell(0,0).value
下面是一个完整的demo
# -*- coding:utf8 -*-
import xlwt
import xlrd
def wirte_excel(list_host_uid, list_uid, list_column, list_roomid, save_file):
"""写excel函数
把list_host_uid, list_uid, list_column, list_roomid值依次填到一行excel中
save_file是要保存的文件
"""
workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.add_sheet('My Worksheet', cell_overwrite_ok=True)
j = 0
for column in list_column:
worksheet.write(0, j, column)
j = j + 1
price = 2
uid_length = len(list_uid)
i = 0
while(i < uid_length-1):
worksheet.write(i+1, 0, list_uid[i])
worksheet.write(i+1, 1, list_uid[i+1])
worksheet.write(i+1, 2, price)
worksheet.write(i+1, 3, list_roomid[i%14])
worksheet.write(i+1, 4, list_host_uid[i%103])
price = price + 2
i = i + 1
host_uid_length = len(list_host_uid)
price = 2
temp = 0
while(temp < host_uid_length):
worksheet.write(i, 0, list_uid[temp])
worksheet.write(i, 1, list_host_uid[temp])
worksheet.write(i, 2, price)
worksheet.write(i, 3, list_roomid[temp%14])
worksheet.write(i, 4, list_host_uid[temp])
price = price + 2
i = i + 1
temp = temp + 1
workbook.save(save_file)
def write_excel_2():
file = xlwt.Workbook()
table = file.add_sheet('sheet name')
table.write(0,0,'test')
#table = file.add_sheet('sheet name',cell_overwrite_ok=True)
file.save('demo.xls')
def writeFile(save_file, txtFile):
"""读excel表,并把它以txt形式存下来
save_file, txtFile分别表示excel文件和txt文件
"""
data = xlrd.open_workbook(save_file)
table = data.sheets()[0]
nrows = table.nrows
file_object = open(txtFile,'w')
i = 0
# \r 是回车,return
# \n 是换行,newline
while(i < nrows):
for j in table.row_values(i):
file_object.write(str(j)[0:-2]+' ')
file_object.write('\n')
i = i + 1
# print table.row_values(2)
# for j in table.row_values(2):
# print str(j)[0:-2]
file_object.close()
def readFile(filename):
"""从txt文件中读取值并以list形式返回
filename表示txt文件
返回值形式为list
"""
listNumber = []
fobj = open(filename,'rb')
for line in fobj.readlines():
#print line
if line.strip() == '':
continue
#print line
listNumber.append(line)
fobj.close()
return listNumber
if __name__ == '__main__':
"""函数的目的就是从txt文件中读取值,然后以一定形式保存在excel中,然后读取excel内容,按行保存到txt文件中
"""
host_uid = "C:\\Users\\xxx\\Desktop\\list_host_uid.txt"
uid = "C:\\Users\\xxx\\Desktop\\list_uid.txt"
txtFile = "C:\\Users\\xxx\\Desktop\\uid.txt"
list_host_uid = readFile(host_uid)
list_uid = readFile(uid)
save_file = "C:\\Users\\xxx\\Desktop\\uid.xls"
list_column = ['uid', 'to_uid', 'price', 'room_id', 'host_uid']
list_roomid = [900160, 900161, 900162, 900163, 900164, 900165, 900150, 900151, 900152, 900153, 900154, 900156, 900157, 900159]
wirte_excel(list_host_uid, list_uid, list_column, list_roomid, save_file)
writeFile(save_file, txtFile)
终身学习