import xlrd
import xlwt
book = xlrd.open_workbook('cccc.xls')
table = book.sheets()[0] # 获取第一个sheet
rows = table.nrows # 获取表格总行数
cols = table.ncols # 获取表格总列数
print('sheet页名称:',book.sheet_names())
print('表格总行数:%d'%rows)
print('表格总列数:%d'%cols)
# table = book.sheets()[0] # 获取第一个sheet
row2_value = table.row_values(1) # 获取第二行的值
print('第2行的值',row2_value)
col2_value = table.col_values(1) #获取第2列的值
print('第2列的值',col2_value)
cell_3_3= table.cell(2,2).value
print('第3行第3列的值',cell_3_3)
workbook = xlwt.Workbook() # 打开excel
worksheet = workbook.add_sheet('test') # 添加一个sheet,命名为test
worksheet.write(0,0,'米好') # 第1行1列单元格写入你好
workbook.save('ewdd.xls')
#psheet = book.sheet_by_index(0)rint('第三行内容为:',sheet.row_values(2))
'''
print('第二列内容为%s,数据类型为%s.'%(sheet.col_values(1),type(sheet.col_values(1))))
print('第二列内容为%s,数据类型为%s.'%(sheet.col(1),type(sheet.col(1))))
print('第二行第二列的单元格内容为:',sheet.cell_value(1,1))
print('第三行第二列的单元格内容为:',sheet.cell(2,1).value)
print('第五行第三列的单元格内容为:',sheet.row(4)[2].value)
print('第五行第三列的单元格内容为%s,数据类型为%s'%(sheet.col(2)[4].value,type(sheet.col(2)[4].value)))
print('第五行第三列的单元格内容为%s,数据类型为%s'%(sheet.col(2)[4],type(sheet.col(2)[4])))
'''
利用xlrd包和xlwt包进行excel文件的读写
最新推荐文章于 2024-03-08 16:20:23 发布
该博客主要展示了如何使用Python的xlrd和xlwt库进行Excel文件的读取与写入操作。首先通过xlrd库打开并读取了一个名为'cccc.xls'的工作簿,获取了表格的总行数和总列数,并打印了第二行和第二列的值。接着,创建了一个新的Excel文件'ewdd.xls',在其中添加了一个名为'test'的sheet,并在第一行第一列写入了'米好'。内容涵盖了基本的Excel数据读取和写入,适合初学者了解Python处理Excel的基础知识。
211

被折叠的 条评论
为什么被折叠?



