资讯内容
python如何与excel结合
python与excel结合的方法:首先获取工作表的方法和属性,并创建或删除工作表;然后定位单元格并访问;**后使用函数【copy_worksheet】拷贝工作表即可。f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
python与excel结合的方法:f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
step1 使用load_workbook(r’xlsx文件路径‘)f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
>>> import openpyxl >>> wb = openpyxl.load_workbook(r'D:PycharmProjects equests250.xlsx') >>> type(wb) <class 'openpyxl.workbook.workbook.Workbook'>相关学习推荐:python视频教程f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
step2 获取工作表的方法和属性f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
get_sheet_names()或者sheetnamesf4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
>>> wb.get_sheet_names() ['Sheet'] >>> wb.sheetnames ['Sheet'] >>> ws = wb.get_sheet_by_name('Sheet') # 工作表对象step3 创建和删除工作表f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
create_sheet 创建工作表f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
remove_sheet 删除工作表(删除工作表的对象)f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
>>> rnx = wb.create_sheet(index = 0,title = 'sheet1') >>> wb.get_sheet_names() ['sheet1', 'Sheet'] # 注意删除工作表时,要删除工作表的对象 ws = wb.get_sheet_by_name('工作表') >>> wb.remove_sheet(wb.get_sheet_by_name('sheet1')) >>> wb.sheetnames ['Sheet']step4 定位单元格f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
row 行f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
column 列f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
coordinate 坐标f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
offset 偏移 offset(行偏移,列偏移)f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
>>> c = ws['A2'] >>> c.row 2 >>> c.column 'A' >>> c.coordinate 'A2' >>> d = c.offset(2,0) >>> d.value '这个杀手不太冷'step5 ’AA‘是多少f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
openpyxl.cell.cell.get_column_letter()f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
openpyxl.cell.cell.column_index_from_string()f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
>>> openpyxl.cell.cell.get_column_letter(27) 'AA' >>> openpyxl.cell.cell.column_index_from_string('AA') 27step6 访问多个单元格f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
先迭代行再去迭代列f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
>>> for each_movies in ws['A2':'B10']: # each_movies是一个元祖 for each_cell in each_movies: print(each_cell.value,end = ' ') print(' ') 肖申克的救赎 9.6 霸王别姬 9.6 这个杀手不太冷 9.4 阿甘正传 9.4 美丽人生 9.5 泰坦尼克号 9.3 千与千寻 9.3 辛德勒的名单 9.5 盗梦空间 9.3>>> for each_rows in ws.rows: print(each_rows[1].value) 评分 9.6 9.6 9.4 9.4 .... 8.6还可以指定迭代多少个f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
>>> for each_row in ws.iter_rows(min_row = 2,min_col = 1,max_row = 4,max_col = 2): print(each_row[0].value) 肖申克的救赎 霸王别姬 这个杀手不太冷step7 拷贝工作表f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
copy_worksheet(工作表)f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
>>> new = wb.copy_worksheet(ws) >>> type(new) <class 'openpyxl.worksheet.worksheet.Worksheet'> >>> wb.save(r'D:PycharmProjects equests250.xlsx') #注意先把原先打开的excel文件关闭再去运行代码相关学习推荐:excel基础教程f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
以上就是python如何与excel结合的详细内容,更多请关注少儿编程网其它相关文章!f4B少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
- 上一篇
python如何将一组数组变成图像
简介python中将一组数组变为图像的方法:1、使用scipy.misc将Numpy数组保存为图像import scipy.miscmisc.imsave('out.jpg', image_array)上面的scipy版本会标准化所有图像,以便min(数据)变成黑色,
- 下一篇
Python怎么接收手机短信?
简介python解决接口测试获取手机验证码问题的方法:Android在收到短信后会发送一个Action为android.provider.Telephony.SMS_RECEIVED的广播,所以我们只需要写个类继承BroadcastReceiver就可以很容易地监听到短信。package co