import xlwt
book = xlwt.Workbook(encoding='utf-8', style_compression=0)
"""
创建一个Worlbook对象,相当于创建一个Excel文件
"""
sheet = book.add_sheet('test1',cell_overwrite_ok=True)
"""
创建好的excel文件一般有sheet1,sheet2,sheet3
此处表示将sheet1命名为test
"""
sheet1 = book.add_sheet('test2',cell_overwrite_ok=True)
sheet.write(0,0,'Englishname')
sheet.write(1,0,'Marcovaldo')
"""
0,0表示第0行,第0列
"""
txt1 ='中文名字'
sheet.write(0,1,txt1)
book.save(r'test.xls') #保存
import xlrd
filepath = "test.xls"
book = xlrd.open_workbook(filepath) #实例化一个对象
name = book.sheet_names() #返回所有sheet的列表
sheet2 = book.sheet_by_index(0) #以索引来获取表
sheet1 = book.sheet_by_name(name[0]) #以名字来获取表
print(sheet1,sheet2)
print(sheet1.name,sheet2.nrows,sheet1.ncols) #返回sheet的名字,行数,列数
cc = sheet1.get_rows() #返回一个迭代器,遍历所有行,给出每个行的值列表
print(cc.__next__())
print(cc.__next__())
print(sheet1.row_values(0)) #返回某一行的值列表
row = sheet1.row(1) #返回一个row对象,可以通过row[index]来获取这行里的单元格cell对象
print(row[0])
print(sheet1.col_values(0)) #返回某一列的值列表
print(sheet1.cell(0,0)) #获取一个cell对象(row和col都从0开始算)
参考内容:
https://www.cnblogs.com/beginner-boy/p/7239696.html
https://www.cnblogs.com/franknihao/p/6535980.html