How to combine python with excel

coldplay.xixi
Release: 2020-08-11 15:37:40
Original
10369 people have browsed it

python与excel结合的方法:首先获取工作表的方法和属性,并创建或删除工作表;然后定位单元格并访问;最后使用函数【copy_worksheet】拷贝工作表即可。

How to combine python with excel

python与excel结合的方法:

step1 使用load_workbook(r’xlsx文件路径‘)

>>> import openpyxl
>>> wb = openpyxl.load_workbook(r'D:\PycharmProjects\requests\250.xlsx')
>>> type(wb)
<class &#39;openpyxl.workbook.workbook.Workbook&#39;>
Copy after login

相关学习推荐:python视频教程

step2 获取工作表的方法和属性

get_sheet_names()或者sheetnames

>>> wb.get_sheet_names()
[&#39;Sheet&#39;]
>>> wb.sheetnames
[&#39;Sheet&#39;]
>>> ws = wb.get_sheet_by_name(&#39;Sheet&#39;)  # 工作表对象
Copy after login

step3 创建和删除工作表

create_sheet 创建工作表

remove_sheet 删除工作表(删除工作表的对象)

>>> rnx = wb.create_sheet(index = 0,title = &#39;sheet1&#39;)
>>> wb.get_sheet_names()
[&#39;sheet1&#39;, &#39;Sheet&#39;]
#  注意删除工作表时,要删除工作表的对象  ws = wb.get_sheet_by_name(&#39;工作表&#39;)
>>> wb.remove_sheet(wb.get_sheet_by_name(&#39;sheet1&#39;))
>>> wb.sheetnames
[&#39;Sheet&#39;]
Copy after login

step4 定位单元格

row 行

column 列

coordinate 坐标

offset 偏移 offset(行偏移,列偏移)

>>> c = ws[&#39;A2&#39;]
>>> c.row 
2
>>> c.column
&#39;A&#39;
>>> c.coordinate
&#39;A2&#39;
>>> d = c.offset(2,0)
>>> d.value
&#39;这个杀手不太冷&#39;
Copy after login

step5 ’AA‘是多少

openpyxl.cell.cell.get_column_letter()

openpyxl.cell.cell.column_index_from_string()

>>> openpyxl.cell.cell.get_column_letter(27)
&#39;AA&#39;
>>> openpyxl.cell.cell.column_index_from_string(&#39;AA&#39;)
27
Copy after login

step6 访问多个单元格

先迭代行再去迭代列

>>> for each_movies in ws[&#39;A2&#39;:&#39;B10&#39;]: # each_movies是一个元祖
    for each_cell in each_movies:
        print(each_cell.value,end = &#39; &#39;)
    print(&#39;\n&#39;)
    
肖申克的救赎 9.6 
霸王别姬 9.6 
这个杀手不太冷 9.4 
阿甘正传 9.4 
美丽人生 9.5 
泰坦尼克号 9.3 
千与千寻 9.3 
辛德勒的名单 9.5 
盗梦空间 9.3
Copy after login
>>> for each_rows in ws.rows:
    print(each_rows[1].value)
    
评分
9.6
9.6
9.4
9.4
....
8.6
Copy after login

还可以指定迭代多少个

>>> for each_row in ws.iter_rows(min_row = 2,min_col = 1,max_row = 4,max_col = 2):
    print(each_row[0].value)
    
肖申克的救赎
霸王别姬
这个杀手不太冷
Copy after login

step7 拷贝工作表

copy_worksheet(工作表)

>>> new = wb.copy_worksheet(ws)
>>> type(new)
<class &#39;openpyxl.worksheet.worksheet.Worksheet&#39;>
>>> wb.save(r&#39;D:\PycharmProjects\requests\250.xlsx&#39;) #注意先把原先打开的excel文件关闭再去运行代码
Copy after login

相关学习推荐:excel基础教程

The above is the detailed content of How to combine python with excel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!