PythonClassChy/python27Class/unitTest/class03072020/demo3/pythonExcel.py

77 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
=================================
Author: Flora Chen
Time: 2020/3/7 11:03
-_- -_- -_- -_- -_- -_- -_- -_-
=================================
"""
import openpyxl
# 将指定的excel文件加载为一个workbook对象
wb = openpyxl.load_workbook('case.xlsx')
# 选中工作簿中的表单对象
sh = wb['case']
# 写入数据
sh.cell(row=3, column=1, value='flora')
# 将工作簿对象保存为文件
wb.save('case.xlsx')
# 获取表单中的最大列
max_column= sh.max_column
print(max_column)
# 获取表单中的最大行
max_row = sh.max_row
print(max_row)
# columns按列获取整个表单里面的所有格子对象每行的内容放在一个元组中
res2 = list(sh.columns)
print(res2)
# 通过for循环获取每列中的内容
for item in res2:
for j in item:
print(j.value)
# rows按行获取整个表单里面的所有格子对象每行的内容放在一个元组中
res1 = list(sh.rows)
print(res1)
# 通过for循环获取每行中的内容
for item in res1:
for i in item:
print(i.value)
# 读取指定格子中的数据
c12 = sh.cell(row=1, column=2).value # 表示获取第一行第二列的数据
print('------------------')
# 读取第三行的数据
res3 = list(sh.rows)
print(res3[2])
for i in res3[2]:
print(i.value)
# 读取第二列到第四列的内容
res4 = list(sh.columns)
print(res4[1:3])
for j in res4[1:3]:
for i in j:
print(i.value)