python对excel表格的操作-创新互联

# 工作簿, 工作表,单元格
#workbook ,sheet ,cell
# 灵活擦欧总各种对象,进行修改‘
# 编辑样式
%cd D:\python全站\office
D:\python全站\office
# pip install openpyxl
import openpyxl
wb = 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.

sh2 = wb.active
cell1 = sh2['A1']
print(cell1)
cell1.value


'学生'
sh2['A1'].value
'学生'
sh2['A2'].coordinate
'A2'
sh2['A2'] = 'zhao'  # another sh2['A2'].value = 'zhao'
sh2['A2'].value
'zhao'
###########
sh2.title
'Sheet2'
sh2.title = '成绩单'
sh2.title
'成绩单'
wb.save('coop-1.xlsx')  # 另存为
##########
# 取sheet1的数据,放入sheet2,成绩> 65
# 打开工作簿
%cd D:\python全站\office
import openpyxl
wb = openpyxl.load_workbook('coop.xlsx')
# 打开sheet1,打开sheet2

#操作sheet1,存入sheet2
# 另存为新的文件
D:\python全站\office
# sh2 = wb.get_sheet_by_name('Sheet1')  # 首写大写
sh2 = wb['Sheet1']  #对Sheet1页面操作
sh3 = wb['Sheet2']  #对Sheet2页面操作
for row in sh2.rows:  # 循环每一行 sh2.row()
    print(row)
    print(row[0].value, row[1].value)   # 打印的是元祖
(, )
学生 成绩
(, )
coop 60
(, )
murphy 61
(, )
lisi 62
(, )
zhangsan 63
(, )
lilei 64
(, )
××× 65
(, )
hao 66
for rows in sh2.rows:
    if rows[0].coordinate != 'A1':   #元祖用法
        #rows[0].coordinate去坐标,不等于A1
        print(rows[0].value, rows[1].value)
coop 60
murphy 61
lisi 62
zhangsan 63
lilei 64
××× 65
hao 66
for rows in sh2.rows:
    if rows[0].coordinate != 'A1' and rows[1].value >63: 
        #rows[0].coordinate去坐标,不等于A1
        print(rows[0].value, rows[1].value)
lilei 64
××× 65
hao 66
index = 2
for rows in sh2.rows:
    if rows[0].coordinate != 'A1' and rows[1].value >63: 
        #rows[0].coordinate去坐标,不等于A1
        print(rows[0].value, rows[1].value)
        sh3['A' + str(index)] = rows[0].value
        sh3['B' + str(index)] = rows[1].value
        print('in sh3:', sh3['A'+str(index)].value,sh3['B'+ str(index)].value)
        index += 1
wb.save('coop-2.xlsx')
lilei 64
in sh3: lilei 64
××× 65
in sh3: ××× 65
hao 66
in sh3: hao 66
# 第二种写法,根据范围取值
#A2 B2
# A3, B3
# index = 2
sh2 = wb['Sheet1']
sh3 = wb['Sheet3']
for rows in range(2, sh2.max_row +1):
    grade = sh2.cell(row = rows, column = 2).value
#     print(grade)
#     print(type(grade))
    if grade > 63:
        sh3['A' + str(rows)] = sh2.cell(row = rows, column = 1).value
        sh3['B' + str(rows)] = grade
        print('in sh3:', sh3['A'+str(index)].value,sh3['B'+ str(index)].value)

wb.save('coop-3.xlsx')
in sh3: None None
in sh3: None None
in sh3: None None
print(sh2.max_row)
8
# 最后一行添加平均分数
sh2.cell(row=9, column=2).value = '=average(B2:B8)'
sh2.cell(row=9, column=1).value = '平均分'
print(sh2['B10'].value)
wb.save('coop-4.xlsx')
None
print(sh2['B9'].value)
=average(B2:B8)
from openpyxl.styles import Font
# Font?
font = Font(bold =True, size = 20) # name, size, bold, italic...
sh2['B9'].font = font

wb.save('coop-5.xlsx')

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

“只有客户发展了,才有我们的生存与发展!”这是创新互联建站的服务宗旨!把网站当作互联网产品,产品思维更注重全局思维、需求分析和迭代思维,在网站建设中就是为了建设一个不仅审美在线,而且实用性极高的网站。创新互联对成都做网站、成都网站建设、网站制作、网站开发、网页设计、网站优化、网络推广、探索永无止境。
网页题目:python对excel表格的操作-创新互联
文章起源:http://ybzwz.com/article/iepec.html