当前位置:   article > 正文

openpyxl 插入列_[Python]Excel编程(openpyxl)学习笔记

openpyxl 添加列

1、基本概念

在openpyxl中,主要用到三个概念:Workbooks,Sheets,Cells。

Workbook就是一个excel工作表;

Sheet是工作表中的一张表页;

Cell就是简单的一个格。

openpyxl就是围绕着这三个概念进行的,不管读写都是“三板斧”:

打开Workbook,定位Sheet,操作Cell。

下面分读和写分别介绍几个常见的方法。

1.1 打开 Excel 表格并获取表格名称

from openpyxl import load_workbook

workbook = load_workbook(filename ="test.xlsx")

workbook.sheetnames

c5b7386e975bad67780f9a9c96a2490d.png

1.2 通过 sheet 名称获取表格

from openpyxl import load_workbook

workbook = load_workbook(filename ="test.xlsx")

workbook.sheetnames

sheet = workbook["Sheet1"]

print(sheet)

ca3b5699ca80f777c4e02fbbb552a9f7.png

1.3 获取表格的尺寸大小 几行几列数据

sheet.dimensions

33d40d6c8514df4e1f5f1645dbc06e42.png

1.4 获取表格内某个格子的数据

workbook = load_workbook(filename ="test.xlsx")

sheet = workbook.active

print(sheet)

cell1 = sheet["A1"]

cell2 = sheet["C11"]

print(cell1.value, cell2.value)

2e37c67fdc635d05f00dee08a9976887.png

workbook = load_workbook(filename ="test.xlsx")

sheet = workbook.active

print(sheet)

cell1 = sheet.cell(row = 1,column = 1)

cell2 = sheet.cell(row = 11,column = 3)

print(cell1.value, cell2.value)

ceb2fe021f49e6e6a7e7e469f88aabfd.png

1.5 获取某个格子的行数、列数、坐标

workbook = load_workbook(filename ="test.xlsx")

sheet = workbook.active

print(sheet)

cell1 = sheet["A1"]

cell2 = sheet["C11"]

print(cell1.value, cell1.row, cell1.column,cell1.coordinate)

print(cell2.value, cell2.row, cell2.column,cell2.coordinate)

776192b14ea346dbb6f671088e23f772.png

1.6 获取一系列格子

workbook = load_workbook(filename ="test.xlsx")

sheet = workbook.active

print(sheet)

# 获取A1:C2区域的值

cell = sheet["A1:C2"]

print(cell)

for i in cell:

   for j in i:

           print(j.value)          

5cc52e56b65ad7b06a50102c6869c4b8.png

16036e8f8752f317f00e47e32f5028af.png

sheet["A"] --- 获取A列的数据

sheet["A:C"] --- 获取A,B,C三列的数据

sheet[5] --- 只获取第5行的数据

1.7  按行获取值

for i in sheet.iter_rows(min_row=2,max_row=5, min_col=1, max_col=2):

   for j in i:

           print(j.value)

5971f79d23552f882dee03d2b40ea871.png

1.8 按列获取值

for i in sheet.iter_cols(min_row=2,max_row=5, min_col=1, max_col=2):

   for j in i:

           print(j.value)

a697e9cad0026bd372e361b7df32c447.png

1.9 向某个格子中写入内容并保存

workbook = load_workbook(filename ="test.xlsx")

sheet = workbook.active

print(sheet)

sheet["A1"] = "哈喽"

4e0a11d9b7658de3e85fabf879bd58f6.png

# 这句代码也可以改为cell =sheet["A1"]  cell.value ="哈喽"

fa59bb1bc18d5a0a70307cea1a3923e9.png

workbook.save(filename = "哈喽.xlsx")

1.10 向表格中插入行数据

workbook = load_workbook(filename ="test.xlsx")

sheet = workbook.active

print(sheet)

data = [ ["唐僧","男","180cm"], ["孙悟空","男","188cm"], ["猪八戒","男","175cm"], ["沙僧","男","176cm"], ]

for row in data:

   sheet.append(row)

workbook.save(filename ="test.xlsx")

04a87795e39a1de6bae7ea3b2d37687b.png

1.11 在 python 中使用 excel 函数公式

workbook = load_workbook(filename ="test.xlsx")

sheet = workbook.active

print(sheet)

sheet["D1"] = "标准身高"

for i in range(2,16):

sheet["D{}".format(i)]=                       '=IF(RIGHT(C{},2)="cm",C{},SUBSTITUTE(C{},"m","")*100&"cm")'.format(i,i,i)

workbook.save(filename ="test.xlsx")

1.12 其他命令 

.insert_rowss() 和.insert_ cols():插入空行和空列

.delete_rows() 和.delete_ cols():删除行和列

.move_ range():移动格子

.create_ sheet():创建新的 sheet 表格

. remove():删除某个sheet 表

.copy_ worksheet():复制一个 sheet 表到另外一张 excel 表

sheet.title :修改sheet 表的名称

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/69582?site
推荐阅读
相关标签
  

闽ICP备14008679号