非常钟教会你运用Python操作excel,内附过程和代码
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这篇<span style="color: black;">文案</span><span style="color: black;">重点</span>介绍了python<span style="color: black;">运用</span>openpyxl操作excel的<span style="color: black;">办法</span><span style="color: black;">过程</span>,<span style="color: black;">仔细</span>的介绍了openpyxl的常用属性以及操作excel的<span style="color: black;">办法</span>,感兴趣的<span style="color: black;">能够</span><span style="color: black;">认识</span>一下</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">一 前言</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">放大招了,学完这篇openpyxl第三方库,读者将会懂得<span style="color: black;">怎样</span>灵活的读取excel数据,<span style="color: black;">怎样</span>创建excel工作表;更新工作表,删除工作表;是不是感觉很强大,留下赞赞吧!!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">二 openpyxl常用属性函数</strong></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/74d6055d7a1a4e80b941b1cbf2ddf3ca~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904360&x-signature=9fwC1K4U%2FgJobwHw3NahJDITRWE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">三 读取excel</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">读取整体流程如下</p>加载工作本获取标签页获取指定区域的单元格获取单个单元格对象<span style="color: black;">经过</span>单元格对象获取值<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3.1 准备工作</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">创建一个名为zszxz.xlsx excel文件;其中标签sheet名为zszxz; 列A B C 如下;</p><span style="color: black;">id</span> <span style="color: black;">num</span> <span style="color: black;">name</span>
<span style="color: black;">1</span> <span style="color: black;">100</span> <span style="color: black;">zszxz</span>
<span style="color: black;">2</span> <span style="color: black;">101</span> <span style="color: black;">smile</span>
<span style="color: black;">3</span> <span style="color: black;">102</span> <span style="color: black;">kitty</span>
<span style="color: black;">4</span> <span style="color: black;">103</span> <span style="color: black;">wolf</span>
<span style="color: black;">5</span> <span style="color: black;">104</span> <span style="color: black;">cloud</span>
<span style="color: black;">6</span> <span style="color: black;">105</span> <span style="color: black;">water</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3.2 安装</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">支持操作excel和图像</p><span style="color: black;">pip</span> <span style="color: black;">install openpyxl</span>
<span style="color: black;">pip</span> <span style="color: black;">install pillow</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3.3 获取所有标签页名<span style="color: black;">叫作</span></strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz.xlsx</span>
<span style="color: black;"># 加载工作本</span>
workbook = openpyxl.load_workbook(path)
<span style="color: black;"># <span style="color: black;">得到</span>所有标签页</span>sheet_names = workbook.get_sheet_names()
print(sheet_names)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">正确的获取方式</p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz.xlsx</span>
<span style="color: black;"># 加载工作本</span>workbook = openpyxl.load_workbook(path)<span style="color: black;"># <span style="color: black;">得到</span>所有标签页名<span style="color: black;">叫作</span></span>
print(workbook.sheetnames)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3.4 获取指定标签页对象</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不<span style="color: black;">意见</span><span style="color: black;">经过</span><span style="color: black;">办法</span><span style="color: black;">得到</span>标签页对象,<span style="color: black;">意见</span><span style="color: black;">经过</span>属性方式获取;</p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span>openpyxl
path =<span style="color: black;">rC:\mydata\generator\py\zszxz.xlsx</span>
<span style="color: black;"># 加载工作本</span>
workbook = openpyxl.load_workbook(path)
<span style="color: black;"># <span style="color: black;">得到</span>指定的sheet</span>
sheet = workbook.get_sheet_by_name(<span style="color: black;">zszxz</span>)
print(sheet)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><Worksheet "zszxz"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">正确的获取方式</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># -*- coding: utf-8 -*-import openpyxlpath = rC:\mydata\generator\py\zszxz.xlsx# 加载工作本workbook = openpyxl.load_workbook(path)# 获取sheet对象sheet = workbookprint(sheet)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><Worksheet "zszxz"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当然读者<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">经过</span>循环工作本方式获取sheet,不赘述;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3.5 复制sheet</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在已有的sheet上<span style="color: black;">能够</span>进行复制一个副本;</p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz.xlsx</span>
<span style="color: black;"># 加载工作本</span>
workbook = openpyxl.load_workbook(path)
<span style="color: black;"># 获取sheet对象</span>
sheet = workbook[<span style="color: black;">zszxz</span>]
<span style="color: black;"># 复制sheet</span>
cp_sheet = workbook.copy_worksheet(sheet)
print(cp_sheet)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><Worksheet "zszxz Copy"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3.5 获取指定一个单元格对象</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">指定获取A1单元格对象</p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span>openpyxl
path =<span style="color: black;">rC:\mydata\generator\py\zszxz.xlsx</span>
<span style="color: black;"># 加载工作本</span>
workbook = openpyxl.load_workbook(path)
<span style="color: black;"># 获取sheet对象</span>
sheet = workbook[<span style="color: black;">zszxz</span>]
<span style="color: black;"># 获取指定单元格</span>cell = sheet[<span style="color: black;">A1</span>]
print(cell)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><Cell zszxz.A1></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">方式二</strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz.xlsx</span>
<span style="color: black;"># 加载工作本</span>workbook = openpyxl.load_workbook(path)<span style="color: black;"># 获取sheet对象</span>
sheet = workbook[<span style="color: black;">zszxz</span>]
<span style="color: black;"># 获取指定单元格</span>
cell = sheet.cell(row=<span style="color: black;">1</span>, column=<span style="color: black;">1</span>)
print(cell)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><Cell zszxz.A1></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3.6 <span style="color: black;">拜访</span>多个单元格</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">拜访</span>单元格获取单元格对象;</p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz.xlsx</span>
<span style="color: black;"># 加载工作本</span>
workbook = openpyxl.load_workbook(path)
<span style="color: black;"># 获取sheet对象</span>sheet = workbook[<span style="color: black;">zszxz</span>]
cells_range = sheet[<span style="color: black;">A1</span>:<span style="color: black;">C1</span>]
<span style="color: black;">for</span> cells <span style="color: black;">in</span> cells_range:
<span style="color: black;">for</span> cell <span style="color: black;">in</span> cells:
print(cell)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><Cell zszxz.C1></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">方式二</p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span>openpyxl
path =<span style="color: black;">rC:\mydata\generator\py\zszxz.xlsx</span>
<span style="color: black;"># 加载工作本</span>
workbook = openpyxl.load_workbook(path)
<span style="color: black;"># 获取sheet对象</span>
sheet = workbook[<span style="color: black;">zszxz</span>]
<span style="color: black;"># 获取指定范围的行</span>row = sheet.iter_rows(min_row=<span style="color: black;">1</span>, max_col=<span style="color: black;">3</span>, max_row=<span style="color: black;">1</span>)
<span style="color: black;">for</span> cell <span style="color: black;">in</span> row:
print(cell)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(<Cell zszxz.A1>, <Cell zszxz.B1>, <Cell zszxz.C1>)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sheet.iter_cols(min_row, max_col, max_row)通用的道理,不赘述</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3.7 获取<span style="color: black;">所有</span>行</strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz.xlsx</span>
<span style="color: black;"># 加载工作本</span>workbook = openpyxl.load_workbook(path)<span style="color: black;"># 获取sheet对象</span>
sheet = workbook[<span style="color: black;">zszxz</span>]
<span style="color: black;"># 获取所有行</span>
<span style="color: black;">for</span> row <span style="color: black;">in</span> sheet.rows:
print(row)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(, , )
</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(, , )
</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(, , )
</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(, , )
</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(, , )
</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(, , )
</p>(<Cell zszxz.A7>, <Cell zszxz.B7>, <Cell zszxz.C7>)
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">同理 sheet.columns获取所有列<span style="color: black;">再也不</span>赘述</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3.8 获取值</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">cell.value获取属性值</p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz.xlsx</span>
<span style="color: black;"># 加载工作本</span>
workbook = openpyxl.load_workbook(path)
<span style="color: black;"># 获取sheet对象</span>
sheet = workbook[<span style="color: black;">zszxz</span>]
cells_range = sheet[<span style="color: black;">A1</span>:<span style="color: black;">C1</span>]
<span style="color: black;">for</span> cells <span style="color: black;">in</span> cells_range:
<span style="color: black;">for</span> cell <span style="color: black;">in</span> cells:
<span style="color: black;"># 获取属性值</span>
print(cell.value)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">id</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">num</p>name
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">四 写入 excel</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4.1 写入文本</strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz_write.xlsx</span>
<span style="color: black;"># 创建一个工作本</span>workbook = openpyxl.Workbook()<span style="color: black;"># 创建一个 sheet</span>
sheet = workbook.create_sheet(<span style="color: black;">zszxz</span>)
<span style="color: black;"># 写入文本形式</span>
sheet[<span style="color: black;">A1</span>] = <span style="color: black;">zszxz666</span>
<span style="color: black;"># 读取</span>
print(sheet[<span style="color: black;">A1</span>].value)
<span style="color: black;"># <span style="color: black;">保留</span></span>workbook.save(path)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">zszxz666</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4.2 写入数字</strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
<span style="color: black;">import</span> datetime
path = <span style="color: black;">rC:\mydata\generator\py\zszxz_write.xlsx</span>
<span style="color: black;"># 创建一个工作本</span>workbook = openpyxl.Workbook()<span style="color: black;"># 创建一个 sheet</span>
sheet = workbook.create_sheet(<span style="color: black;">zszxz</span>)
<span style="color: black;"># 写入值数字形式</span>
sheet[<span style="color: black;">A2</span>] = datetime.datetime(<span style="color: black;">2020</span>, <span style="color: black;">10</span>, <span style="color: black;">15</span>)
print(sheet[<span style="color: black;">A2</span>].value)
<span style="color: black;"># <span style="color: black;">保留</span></span>workbook.save(path)输出<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4.3 写入公式</strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz_write.xlsx</span>
<span style="color: black;"># 创建一个工作本</span>
workbook = openpyxl.Workbook()
<span style="color: black;"># 创建一个 sheet</span>
sheet = workbook.create_sheet(<span style="color: black;">zszxz</span>)
<span style="color: black;"># 写入公式</span>
sheet[<span style="color: black;">A3</span>] = <span style="color: black;">=SUM(1, 1)</span>
print(sheet[<span style="color: black;">A3</span>].value)
<span style="color: black;"># <span style="color: black;">保留</span></span>
workbook.save(path)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4.5 取消合并单元格</strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz_write.xlsx</span>
<span style="color: black;"># 创建一个工作本</span>
workbook = openpyxl.Workbook()
<span style="color: black;"># 创建一个 sheet</span>sheet = workbook.create_sheet(<span style="color: black;">zszxz</span>)
<span style="color: black;"># 取消合并单元格</span>
sheet.unmerge_cells(<span style="color: black;">A2:D2</span>)
<span style="color: black;"># <span style="color: black;">保留</span></span>
workbook.save(path)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4.5 <span style="color: black;">插进</span><span style="color: black;">照片</span></strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
<span style="color: black;">from</span> openpyxl.drawing.image <span style="color: black;">import</span>Image
path =<span style="color: black;">rC:\mydata\generator\py\zszxz_write.xlsx</span>
<span style="color: black;"># 创建一个工作本</span>
workbook = openpyxl.Workbook()
<span style="color: black;"># 创建一个 sheet</span>
sheet = workbook.create_sheet(<span style="color: black;">zszxz</span>)
<span style="color: black;"># 设置图像</span>img = Image(<span style="color: black;">rC:\mydata\generator\py\main.jpg</span>)
<span style="color: black;"># 设置图像单元格说明</span>
sheet[<span style="color: black;">A1</span>] = <span style="color: black;">you are my angel</span>
<span style="color: black;"># <span style="color: black;">插进</span><span style="color: black;">照片</span></span>
sheet.add_image(img, <span style="color: black;">A1</span>)
<span style="color: black;"># <span style="color: black;">保留</span></span>
workbook.save(path)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果如下:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/55413b8dfefa4eacb65bb222578c79e3~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904360&x-signature=AKbUn%2F3gS7u56%2FjWLvS5fgw7XjA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <strong style="color: blue;">4.6 <span style="color: black;">隐匿</span>轮廓</strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz_write.xlsx</span>
wb = openpyxl.Workbook()
ws = wb.create_sheet()
<span style="color: black;"># <span style="color: black;">隐匿</span>行 A-B</span>ws.column_dimensions.group(<span style="color: black;">A</span>, <span style="color: black;">B</span>, hidden=<span style="color: black;">True</span>)
<span style="color: black;"># <span style="color: black;">隐匿</span> 列 1 -5 </span>
ws.row_dimensions.group(<span style="color: black;">1</span>, <span style="color: black;">5</span>, hidden=<span style="color: black;">True</span>)
wb.save(path)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4.7 设置行高列高</strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span>openpyxl
path =<span style="color: black;">rC:\mydata\generator\py\zszxz_write.xlsx</span>
<span style="color: black;"># 创建一个工作本</span>
workbook = openpyxl.Workbook()
<span style="color: black;"># 创建一个 sheet</span>
sheet = workbook.create_sheet(<span style="color: black;">zszxz</span>)
<span style="color: black;"># 写入文本形式</span>sheet[<span style="color: black;">A1</span>] = <span style="color: black;">zszxz666</span>
<span style="color: black;"># 设置行高</span>
sheet.row_dimensions[<span style="color: black;">1</span>].height = <span style="color: black;">50</span>
<span style="color: black;"># 设置列高</span>
sheet.column_dimensions[<span style="color: black;">A</span>].width = <span style="color: black;">30</span>
workbook.save(path)
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">五 删除sheet</strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz_write.xlsx</span>
<span style="color: black;"># 加载工作本</span>
workbook = openpyxl.load_workbook(path)
<span style="color: black;"># <span style="color: black;">得到</span>sheet</span>
sheet = workbook[<span style="color: black;">Sheet</span>]
<span style="color: black;"># 移除sheet</span>
workbook.remove(sheet)
<span style="color: black;"># <span style="color: black;">保留</span></span>
workbook.save(path)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">六 更新工作表</strong></p><span style="color: black;"># -*- coding: utf-8 -*-</span>
<span style="color: black;">import</span> openpyxl
path = <span style="color: black;">rC:\mydata\generator\py\zszxz_write.xlsx</span>
<span style="color: black;"># 加载工作本</span>workbook = openpyxl.load_workbook(path)<span style="color: black;"># <span style="color: black;">得到</span>sheet</span>
sheet = workbook[<span style="color: black;">zszxz</span>]
<span style="color: black;"># <span style="color: black;">得到</span>值</span>
val = sheet[<span style="color: black;">A1</span>].value
print(val)
<span style="color: black;"># 重新赋值</span>
new_val = sheet[<span style="color: black;">A1</span>].value = <span style="color: black;">zszxz</span>print(new_val)<span style="color: black;"># <span style="color: black;">保留</span></span>
workbook.save(path)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">zszxz666</p>zszxz
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,<span style="color: black;">博主</span>想说:我是一名python<span style="color: black;">研发</span>工程师,整理了一套最新的python系统学习教程,想要这些资料的<span style="color: black;">能够</span>关注私信<span style="color: black;">博主</span>“01”<span style="color: black;">就可</span>,<span style="color: black;">期盼</span>能对你有所<span style="color: black;">帮忙</span>。</p>
谷歌网站排名优化 http://www.fok120.com/ 我完全同意你的观点,说得太对了。 我完全赞同你的观点,思考很有深度。
页:
[1]