Python excel 功能扩展库 ——> openpyxl 的基本使用

时间:2019-03-13
本文章向大家介绍Python excel 功能扩展库 ——> openpyxl 的基本使用,主要包括Python excel 功能扩展库 ——> openpyxl 的基本使用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
from openpyxl import Workbook

from openpyxl import load_workbook
# 实例化一个操作对象
wb = Workbook()

# 获取当前活跃状态的sheet
ws = wb.active

'''
# 基本操作,插入方式按顺序逐行插入
# 单元格内容控制
ws['A1'] = '姓名'
ws['B1'] = '性别'
# 在上文基础上添加整行(上文存在三行,则从第四行开始整行插入)
ws.append(['项目','姓名','时间','报价','备注'])
ws.append([1,2,3])
ws.append([23,34,45,56])
# 单元格内容控制
ws['A2'] = 'jony'
ws['A3'] = 'male'
# 文件保存(必须用绝对路径)
wb.save('/home/ht/fir.xlsx')
'''

'''
# 创建新的工作区,并输入该区的标签名(标签名,位置)* 位置:该区在标签中的排序
w1 = wb.create_sheet('sheet1')
w2 = wb.create_sheet('sheet2')
w3 = wb.create_sheet('sheet3',0)

# 这里是修改该区标签名
w1.title = 'sheet-1'
w2.title = 'sheet-2'
w3.title = 'sheet-3'

w2.append(['ds','hp','wq'])

# 设置标签的背景色(不是表格的单元格)
w2.sheet_properties.tabColor = "1072BA"

# 可以通过工作区的title名来获取该sheet对象
wanna = wb.get_sheet_by_name('sheet-2')

print(wanna)
print(wb.sheetnames)

# <Worksheet "sheet-2">
# ['sheet-3', 'Sheet', 'sheet-1', 'sheet-2']

wb['sheet-3'].append([1,2,3,4,5])

# 复制工作区,新的工作区的默认命名为sheet-3 Copy
new_3 = wb.copy_worksheet(w3)
# 复制品重命名
new_3.title = 'new'
wb.save('/home/ht/mul_sheet.xlsx')
'''

'''
sh1 = wb.active
sh2 = wb.create_sheet('sheet-cell')

# 单表格坐标数值输入
sh2['A1'] = 'aaa插入内容'
# 单元格坐标接受小写
sh2['d4'] = '表格小写'
# 单元格行列值坐标输入
cell1 = sh2.cell(row=3,column=2,value='三行二列')
cell2 = sh2.cell(3,4,'三行四列')
print(cell1.value)
# 三行二列
wb.save('/home/ht/sheet-cell.xlsx')
'''

'''
# 批量获取单元格
mul_cell = wb.active
mul_cell.append(['a1','b1','c1','d1'])
mul_cell.append(['a2','b2','c2','d2'])
mul_cell.append(['a3','b3','c3','d3'])

# 获取A列所有单元格
print(mul_cell['a'])
# (<Cell 'Sheet'.A1>,
# <Cell 'Sheet'.A2>,
# <Cell 'Sheet'.A3>)

# 获取 BCD 三列所有单元格
print(mul_cell['b:d'])
# ((<Cell 'Sheet'.B1>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.B3>),
# (<Cell 'Sheet'.C1>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.C3>),
# (<Cell 'Sheet'.D1>, <Cell 'Sheet'.D2>, <Cell 'Sheet'.D3>))

# 获取第 2到3 行所有单元格
print(mul_cell[2:3])
# ((<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.D2>),
# (<Cell 'Sheet'.A3>, <Cell 'Sheet'.B3>, <Cell 'Sheet'.C3>, <Cell 'Sheet'.D3>))

# iter_rows 方法,设定参数,获取二行三列区块内的所有单元格(获取基本单位为行,再从行内获取单元格)
for r in mul_cell.iter_rows(min_row=1,max_row=2,min_col=1,max_col=3):
print(r)
for c in r:
print(c,c.value)

# (<Cell 'Sheet'.A1>, <Cell 'Sheet'.B1>, <Cell 'Sheet'.C1>)
# <Cell 'Sheet'.A1> a1
# <Cell 'Sheet'.B1> b1
# <Cell 'Sheet'.C1> c1
# (<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>)
# <Cell 'Sheet'.A2> a2
# <Cell 'Sheet'.B2> b2
# <Cell 'Sheet'.C2> c2

# iter_rows 方法,设定参数,获取二行三列区块内的所有单元格(获取基本单位为列,再从列内获取单元格)
for r in mul_cell.iter_cols(min_row=1,max_row=2,min_col=1,max_col=3):
print(r)

# (<Cell 'Sheet'.A1>, <Cell 'Sheet'.A2>)
# (<Cell 'Sheet'.B1>, <Cell 'Sheet'.B2>)
# (<Cell 'Sheet'.C1>, <Cell 'Sheet'.C2>)

# 获取所有行
for r in mul_cell.rows:
print(r)
# (<Cell 'Sheet'.A1>, <Cell 'Sheet'.B1>, <Cell 'Sheet'.C1>, <Cell 'Sheet'.D1>)
# (<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.D2>)
# (<Cell 'Sheet'.A3>, <Cell 'Sheet'.B3>, <Cell 'Sheet'.C3>, <Cell 'Sheet'.D3>)

print(mul_cell.rows)
# <generator object Worksheet._cells_by_row at 0x7f4c615312b0>

# 获取所有列
print(mul_cell.columns)
# <generator object Worksheet._cells_by_col at 0x7f4c615312b0>
wb.save('/home/ht/mul_cell.xlsx')
'''

'''
from openpyxl import load_workbook

# 操作文件
read_sheet = load_workbook('/home/ht/mul_sheet.xlsx')

read_sheet.guess_types = True

current = read_sheet.active
current['e2'] = '65%'

read_sheet.save('/home/ht/mul_sheet.xlsx')
'''

'''
from openpyxl import load_workbook
wsheet = load_workbook('/home/ht/mul_cell.xlsx')

# sheet 对象不存在脚标,只能通过坐标获取,wsheet.active.rows[1]是不能获取第二行的!
print(wsheet.active['a'][1].value)
print(wsheet.active['2'][1].value)
# a2
# b2
'''

''' 单元格分割合并

deal_cell = load_workbook('/home/ht/sum_sheet.xlsx')
sheet = deal_cell.active

# 合并第一行1到4单元格(a1,b1,c1,d1)
sheet.merge_cells('a1:d1')

# 将 第一行 a1 到 d1 位置的单元格分离出来
sheet.unmerge_cells('a1:d1')

# 采用区块的方式合并或分割操作
sheet.merge_cells(start_row=1,end_row=3,start_column=4,end_column=6)

deal_cell.save('/home/ht/sum_sheet.xlsx')
'''

''' 单元格插入图片

# 需要安装PIL图片包
from openpyxl.drawing.image import Image

img = Image('/home/ht/qcode.png')
ws.add_image(img,'F3') # 这里图片填充的位置坐标必须使用大写字母
wb.save('/home/ht/addimg.xlsx')
'''