Python Operations on Excel tables

Source: Internet
Author: User

# 工作簿, 工作表,单元格#workbook ,sheet ,cell# 灵活擦欧总各种对象,进行修改‘# 编辑样式%cd D:\python全站\office
D:\python全站\office
# pip install openpyxlimport openpyxlwb = openpyxl.load_workbook(‘coop.xlsx‘)  # 加载创建的表格coop.xlsx
wb.get_active_sheet()
c:\users\coop\miniconda3\envs\coop\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: Call to deprecated function get_active_sheet (Use the .active property).  """Entry point for launching an IPython kernel.<Worksheet "Sheet2">
sh1 = wb.active
cell1 = sh1[‘A1‘]
print(cell1)cell1.value
<Cell ‘Sheet2‘.A1>‘学生‘
sh1[‘A1‘].value
‘学生‘
sh1[‘A2‘].coordinate
‘A2‘
sh1[‘A2‘] = ‘zhao‘  # another sh1[‘A2‘].value = ‘zhao‘
sh1[‘A2‘].value
‘zhao‘
###########
sh1.title
‘Sheet2‘
sh1.title = ‘成绩单‘
sh1.title
‘成绩单‘
wb.save(‘coop-1.xlsx‘)  # 另存为
##########
# 取sheet1的数据,放入sheet2,成绩> 65# 打开工作簿%cd D:\python全站\officeimport openpyxlwb = openpyxl.load_workbook(‘coop.xlsx‘)# 打开sheet1,打开sheet2#操作sheet1,存入sheet2# 另存为新的文件
D:\python全站\office
# sh1 = wb.get_sheet_by_name(‘Sheet1‘)  # 首写大写
sh1 = wb[‘Sheet1‘]  #对Sheet1页面操作sh2 = wb[‘Sheet2‘]  #对Sheet2页面操作
for row in sh1.rows:  # 循环每一行 sh1.row()    print(row)    print(row[0].value, row[1].value)   # 打印的是元祖
(<Cell ‘Sheet1‘.A1>, <Cell ‘Sheet1‘.B1>)学生 成绩(<Cell ‘Sheet1‘.A2>, <Cell ‘Sheet1‘.B2>)coop 60(<Cell ‘Sheet1‘.A3>, <Cell ‘Sheet1‘.B3>)murphy 61(<Cell ‘Sheet1‘.A4>, <Cell ‘Sheet1‘.B4>)lisi 62(<Cell ‘Sheet1‘.A5>, <Cell ‘Sheet1‘.B5>)zhangsan 63(<Cell ‘Sheet1‘.A6>, <Cell ‘Sheet1‘.B6>)lilei 64(<Cell ‘Sheet1‘.A7>, <Cell ‘Sheet1‘.B7>)××× 65(<Cell ‘Sheet1‘.A8>, <Cell ‘Sheet1‘.B8>)hao 66
for rows in sh1.rows:    if rows[0].coordinate != ‘A1‘:   #元祖用法        #rows[0].coordinate去坐标,不等于A1        print(rows[0].value, rows[1].value)
coop 60murphy 61lisi 62zhangsan 63lilei 64××× 65hao 66
for rows in sh1.rows:    if rows[0].coordinate != ‘A1‘ and rows[1].value >63:         #rows[0].coordinate去坐标,不等于A1        print(rows[0].value, rows[1].value)
lilei 64××× 65hao 66
index = 2for rows in sh1.rows:    if rows[0].coordinate != ‘A1‘ and rows[1].value >63:         #rows[0].coordinate去坐标,不等于A1        print(rows[0].value, rows[1].value)        sh2[‘A‘ + str(index)] = rows[0].value        sh2[‘B‘ + str(index)] = rows[1].value        print(‘in sh2:‘, sh2[‘A‘+str(index)].value,sh2[‘B‘+ str(index)].value)        index += 1wb.save(‘coop-2.xlsx‘)
lilei 64in sh2: lilei 64××× 65in sh2: ××× 65hao 66in sh2: hao 66
# 第二种写法,根据范围取值#A2 B2# A3, B3# index = 2sh1 = wb[‘Sheet1‘]sh2 = wb[‘Sheet3‘]for rows in range(2, sh1.max_row +1):    grade = sh1.cell(row = rows, column = 2).value#     print(grade)#     print(type(grade))    if grade > 63:        sh2[‘A‘ + str(rows)] = sh1.cell(row = rows, column = 1).value        sh2[‘B‘ + str(rows)] = grade        print(‘in sh2:‘, sh2[‘A‘+str(index)].value,sh2[‘B‘+ str(index)].value)wb.save(‘coop-3.xlsx‘)
in sh2: None Nonein sh2: None Nonein sh2: None None
print(sh1.max_row)
8
# 最后一行添加平均分数sh1.cell(row=9, column=2).value = ‘=average(B2:B8)‘sh1.cell(row=9, column=1).value = ‘平均分‘print(sh1[‘B10‘].value)wb.save(‘coop-4.xlsx‘)
None
print(sh1[‘B9‘].value)
=average(B2:B8)
from openpyxl.styles import Font# Font?font = Font(bold =True, size = 20) # name, size, bold, italic...sh1[‘B9‘].font = fontwb.save(‘coop-5.xlsx‘)

Python Operations on Excel tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.