极客小将

您现在的位置是:首页 » python编程资讯

资讯内容

python如何与excel结合

极客小将2020-11-30-
简介python与excel结合的方法:首先获取工作表的方法和属性,并创建或删除工作表;然后定位单元格并访问;最后使用函数【copy_worksheet】拷贝工作表即可。python与excel结合的方法:step1使用load_workbook(r’xlsx文件路径‘)>>>impo

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') 27

step6 访问多个单元格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_教程_免费儿童编程学习平台

网友点评

共有5条评论来说两句吧...

在线客服