python中和excel_Python和Excel终于可以互通了!!

# coding:utf-8

'''

# 希望对大家有帮助哈,请多提问题

create by yaoyz

date: 2017/01/24

'''

importxlrd

importxlwt

# workbook相关

fromopenpyxl.workbookimportWorkbook

# ExcelWriter,封装了很强大的excel写的功能

fromopenpyxl.writer.excelimportExcelWriter

# 一个eggache的数字转为列字母的方法

fromopenpyxl.utilsimportget_column_letter

fromopenpyxl.reader.excelimportload_workbook

classHandleExcel():

'''Excel相关操作类'''

def__init__(self):

self. head_row_labels = [u'学生ID',u'学生姓名',u'联系方式',u'知识点ID',u'知识点名称']

"""

function:

读出txt文件中的每一条记录,把它保存在list中

Param:

filename: 要读出的文件名

Return:

res_list:返回的记录的list

"""

defread_from_file(self,filename):

res_list=[]

file_obj=open(filename,"r")

forlineinfile_obj.readlines():

res_list.append(line)

file_obj.close()

returnres_list

"""

function:

读出*.xlsx中的每一条记录,把它保存在data_dic中返回

Param:

excel_name: 要读出的文件名

Return:

data_dic:返回的记录的dict

"""

defread_excel_with_openpyxl(self, excel_name="testexcel2007.xlsx"):

# 读取excel2007文件

wb = load_workbook(filename=excel_name)

# 显示有多少张表

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()

# 获取读入的excel表格的有多少行,有多少列

row_num=ws.get_highest_row()

col_num=ws.get_highest_column()

print"row_num: ",row_num," col_num: ",col_num

# 建立存储数据的字典

data_dic = {}

sign=1

# 把数据存到字典中

forrowinws.rows:

temp_list=[]

# print "row",row

forcellinrow:

printcell.value,

temp_list.append(cell.value)

print""

data_dic[sign]=temp_list

sign+=1

printdata_dic

returndata_dic

"""

function:

读出*.xlsx中的每一条记录,把它保存在data_dic中返回

Param:

records: 要保存的,一个包含每一条记录的list

save_excel_name: 保存为的文件名

head_row_stu_arrive_star:

Return:

data_dic:返回的记录的dict

"""

defwrite_to_excel_with_openpyxl(self,records,head_row,save_excel_name="save.xlsx"):

# 新建一个workbook

wb = Workbook()

# 新建一个excelWriter

ew = ExcelWriter(workbook=wb)

# 设置文件输出路径与名称

dest_filename = save_excel_name.decode('utf-8')

# 第一个sheet是ws

ws = wb.worksheets[0]

# 设置ws的名称

ws.title ="range names"

# 写第一行,标题行

forh_xinrange(1,len(head_row)+1):

h_col=get_column_letter(h_x)

#print h_col

ws.cell('%s%s'% (h_col,1)).value ='%s'% (head_row[h_x-1])

# 写第二行及其以后的那些行

i =2

forrecordinrecords:

record_list=str(record).strip().split("\t")

forxinrange(1,len(record_list)+1):

col = get_column_letter(x)

ws.cell('%s%s'% (col, i)).value ='%s'% (record_list[x-1].decode('utf-8'))

i +=1

# 写文件

ew.save(filename=dest_filename)

"""

function:

测试输出Excel内容

读出Excel文件

Param:

excel_name: 要读出的Excel文件名

Return:

"""

defread_excel(self,excel_name):

workbook=xlrd.open_workbook(excel_name)

printworkbook.sheet_names()

# 获取所有sheet

printworkbook.sheet_names()# [u'sheet1', u'sheet2']

sheet2_name = workbook.sheet_names()[1]

# 根据sheet索引或者名称获取sheet内容

sheet2 = workbook.sheet_by_index(1)# sheet索引从0开始

sheet2 = workbook.sheet_by_name('Sheet1')

# sheet的名称,行数,列数

printsheet2.name,sheet2.nrows,sheet2.ncols

# 获取整行和整列的值(数组)

rows = sheet2.row_values(3)# 获取第四行内容

cols = sheet2.col_values(2)# 获取第三列内容

printrows

printcols

# 获取单元格内容

printsheet2.cell(1,0).value

printsheet2.cell_value(1,0)

printsheet2.row(1)[0].value

# 获取单元格内容的数据类型

printsheet2.cell(1,0).ctype

# 通过名称获取

returnworkbook.sheet_by_name(u'Sheet1')

"""

function:

设置单元格样式

Param:

name: 字体名字

height: 字体高度

bold: 是否大写

Return:

style: 返回设置好的格式对象

"""

defset_style(self,name,height,bold=False):

style = xlwt.XFStyle()# 初始化样式

font = xlwt.Font()# 为样式创建字体

font.name = name# 'Times New Roman'

font.bold = bold

font.color_index =4

font.height = height

borders= xlwt.Borders()

borders.left=6

borders.right=6

borders.top=6

borders.bottom=6

style.font = font

style.borders = borders

returnstyle

"""

function:

按照 设置单元格样式 把计算结果由txt转变为Excel存储

Param:

dataset:要保存的结果数据,list存储

Return:

将结果保存为 excel对象中

"""

defwrite_to_excel(self, dataset,save_excel_name,head_row):

f = xlwt.Workbook()# 创建工作簿

# 创建第一个sheet:

# sheet1

count=1

sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)# 创建sheet

# 首行标题:

forpinrange(len(head_row)):

sheet1.write(0,p,head_row[p],self.set_style('Times New Roman',250,True))

default=self.set_style('Times New Roman',200,False)# define style out the loop will work

forlineindataset:

row_list=str(line).strip("\n").split("\t")

forppinrange(len(str(line).strip("\n").split("\t"))):

sheet1.write(count,pp,row_list[pp].decode('utf-8'),default)

count+=1

f.save(save_excel_name)# 保存文件

defrun_main_save_to_excel_with_openpyxl(self):

print"测试读写2007及以后的excel文件xlsx,以方便写入文件更多数据"

print"1. 把txt文件读入到内存中,以list对象存储"

dataset_list=self.read_from_file("test_excel.txt")

'''test use openpyxl to handle EXCEL 2007'''

print"2. 把文件写入到Excel表格中"

head_row_label=self.head_row_labels

save_name="test_openpyxl.xlsx"

self.write_to_excel_with_openpyxl(dataset_list,head_row_label,save_name)

print"3. 执行完毕,由txt格式文件保存为Excel文件的任务"

defrun_main_save_to_excel_with_xlwt(self):

print" 4. 把txt文件读入到内存中,以list对象存储"

dataset_list=self.read_from_file("test_excel.txt")

'''test use xlwt to handle EXCEL 97-2003'''

print" 5. 把文件写入到Excel表格中"

head_row_label=self.head_row_labels

save_name="test_xlwt.xls"

self.write_to_excel_with_openpyxl(dataset_list,head_row_label,save_name)

print"6. 执行完毕,由txt格式文件保存为Excel文件的任务"

if__name__ =='__main__':

print"create handle Excel Object"

obj_handle_excel=HandleExcel()

# 分别使用openpyxl和xlwt将数据写入文件

obj_handle_excel.run_main_save_to_excel_with_openpyxl()

obj_handle_excel.run_main_save_to_excel_with_xlwt()

'''测试读出文件,注意openpyxl不可以读取xls的文件,xlrd不可以读取xlsx格式的文件'''

#obj_handle_excel.read_excel_with_openpyxl("testexcel2003.xls") # 错误写法

#obj_handle_excel.read_excel_with_openpyxl("testexcel2003.xls") # 错误写法

obj_handle_excel.read_excel("testexcel2003.xls")

obj_handle_excel.read_excel_with_openpyxl("testexcel2007.xlsx")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值