1 安装openpyxl
python 中与excel操作相关的模块:
- xlrd库:从excel中读取数据,支持xls、xlsx
- xlwt库:对excel进行修改操作,不支持xlsx格式的修改
- xlutils库:在xlw和xlrd中,对一个已存在的文件进行修改
- openpyxl库:主要针对xlsx格式的excel进行读取和编辑
2 Excel 中的三大对象
- WorkBook:工作对象
- Sheet:表单对象
- Cell:表格对象
3 openpyxl 对 Excel的操作
- 创建一个工作簿: wb = openpyxl.Workbook()
- 新增一个sheet表单:wb.create_sheet(‘sheet2’)
- 保存 demo.xlsx文件:wb.save(‘demo.xlsx’)
- 打开工作簿:wb = openpyxl.load_workbook(‘demo.xlsx’)
- 选取表单:sh = wb[‘sheet2’]
- 读取第一行、第一列的数据:ce = sh.cell(‘row=1, column=1’)
- 按行读取数据:row_data = list(sh.rows)
- 按列读取数据:columns_data = list(sh.clumns)
- 关闭工作簿:wb.close()
3.1 写入数据
写入数据之前,该文件一定要处于关闭状态
value = 'reslut':sh.cell(row=1,column=2,value='result')
- 获取做大行总数、最大列总数:sh.max_row、sh.max_column
- del 删除表单的用法:del wb[‘sheet_name’]
- remove 删除表单的用法:
sh = wb['sheet_name']
wb.remove(sh)
4 excel数据

5 操作案例
5.1 创建excel
import openpyxl
wb = openpyxl.Workbook()
wb.create_sheet('StudentsInfo')
sh = wb['Sheet']
wb.remove(sh)
wb.save('批量导入用户模板.xlsx')
5.2 读取数据
import openpyxl
wb = openpyxl.load_workbook('批量导入用户模板.xlsx')
sh = wb['StudentsInfo']
5.2.1 读取指定单元格数据
ce = sh.cell(row = 1,column = 2)
print(ce.value)
输出:姓名
5.2.2 按行读取数据
print(list(sh.rows)[1:])
for cases in list(sh.rows)[1:]:
stu_id = cases[0].value
stu_name = cases[1].value
stu_sex = cases[2].value
stu_age = cases[3].value
print(stu_id, stu_name, stu_sex, stu_age)
输出:
[
(<Cell 'StudentsInfo'.A2>, <Cell 'StudentsInfo'.B2>, <Cell 'StudentsInfo'.C2>, <Cell 'StudentsInfo'.D2>),
(<Cell 'StudentsInfo'.A3>, <Cell 'StudentsInfo'.B3>, <Cell 'StudentsInfo'.C3>, <Cell 'StudentsInfo'.D3>),
(<Cell 'StudentsInfo'.A4>, <Cell 'StudentsInfo'.B4>, <Cell 'StudentsInfo'.C4>, <Cell 'StudentsInfo'.D4>),
(<Cell 'StudentsInfo'.A5>, <Cell 'StudentsInfo'.B5>, <Cell 'StudentsInfo'.C5>, <Cell 'StudentsInfo'.D5>)
]
1 大佬 男 18
2 高手 女 18
3 大神 男 19
None 6 None None
5.2.2 按行读取数据 结果转为json
all_row = []
for cases in list(sh.rows)[1:]:
row = []
stu_id = cases[0].value
stu_name = cases[1].value
stu_sex = cases[2].value
stu_age = cases[3].value
data = {
"stu_id": stu_id,
"stu_name": stu_name,
"stu_sex": stu_sex,
"stu_age": stu_age
}
all_row.append(data)
print(all_row)
输出:
[
{'stu_id': 1, 'stu_name': '大佬', 'stu_sex': '男', 'stu_age': 18},
{'stu_id': 2, 'stu_name': '高手', 'stu_sex': '女', 'stu_age': 18},
{'stu_id': 3, 'stu_name': '大神', 'stu_sex': '男', 'stu_age': 19},
{'stu_id': None, 'stu_name': 6, 'stu_sex': None, 'stu_age': None}
]
5.2.3 关闭工作薄
wb.close()
5.3 写数据
5.3.1 写入一行
sh = wb['StudentsInfo']
sh.append(['1', '2', '3', '6'])
6 样式
from openpyxl.styles import Font, colors, Alignment
6.1 改变 sheet 标签按钮颜色
二进制颜色代码大全(含图)
sh.sheet_properties.tabColor = "FFAA33"
6.2 设置单元格风格
下面的代码指定了等线24号,加粗斜体,字体颜色蓝色。直接使用cell的font属性,将Font对象赋值给它。
sh = wb['StudentsInfo']
bold_itatic_24_font = Font(name='等线', size=24, italic=True, color=colors.BLUE, bold=True)
sh['A1'].font = bold_itatic_24_font
6.3 对齐方式
也是直接使用cell的属性aligment,这里指定垂直居中和水平居中。除了center,还可以使用right、left等等参数。
sh['B1'].alignment = Alignment(horizontal='center', vertical='center')
6.4 设置行高和列宽
sh.row_dimensions[2].height = 40
sh.column_dimensions['C'].width = 30
6.5 合并和拆分单元格
sh.merge_cells('B1:G1')
sh.merge_cells('A3:D6')
<h2><a id="1_openpyxl_0"></a>1 安装openpyxl</h2>
<p>python 中与excel操作相关的模块:</p>
<ul>
<li>xlrd库:从excel中读取数据,支持xls、xlsx</li>
<li>xlwt库:对excel进行修改操作,不支持xlsx格式的修改</li>
<li>xlutils库:在xlw和xlrd中,对一个已存在的文件进行修改</li>
<li>openpyxl库:主要针对xlsx格式的excel进行读取和编辑</li>
</ul>
<h2><a id="2_Excel__9"></a>2 Excel 中的三大对象</h2>
<ul>
<li>WorkBook:工作对象</li>
<li>Sheet:表单对象</li>
<li>Cell:表格对象</li>
</ul>
<h2><a id="3_openpyxl__Excel_15"></a>3 openpyxl 对 Excel的操作</h2>
<ul>
<li>创建一个工作簿: wb = openpyxl.Workbook()</li>
<li>新增一个sheet表单:wb.create_sheet(‘sheet2’)</li>
<li>保存 demo.xlsx文件:wb.save(‘demo.xlsx’)</li>
<li>打开工作簿:wb = openpyxl.load_workbook(‘demo.xlsx’)</li>
<li>选取表单:sh = wb[‘sheet2’]</li>
<li>读取第一行、第一列的数据:ce = sh.cell(‘row=1, column=1’)</li>
<li>按行读取数据:row_data = list(sh.rows)</li>
<li>按列读取数据:columns_data = list(sh.clumns)</li>
<li>关闭工作簿:wb.close()</li>
</ul>
<h3><a id="31__27"></a>3.1 写入数据</h3>
<p>写入数据之前,该文件一定要处于关闭状态</p>
<ul>
<li>写入第一行、第二列数据:</li>
</ul>
<pre><div class="hljs"><code class="lang-python">value = <span class="hljs-string">'reslut'</span>:sh.cell(row=<span class="hljs-number">1</span>,column=<span class="hljs-number">2</span>,value=<span class="hljs-string">'result'</span>)
</code></div></pre>
<ul>
<li>获取做大行总数、最大列总数:sh.max_row、sh.max_column</li>
<li>del 删除表单的用法:del wb[‘sheet_name’]</li>
<li>remove 删除表单的用法:</li>
</ul>
<pre><div class="hljs"><code class="lang-python">sh = wb[<span class="hljs-string">'sheet_name'</span>]
wb.remove(sh)
</code></div></pre>
<h2><a id="4_excel_46"></a>4 excel数据</h2>
<p><img src="https://static.couragesteak.com/article/83c90c0f1e9c9e2523ebfc71a0c1c839.png" alt="image.png" /></p>
<h2><a id="5__50"></a>5 操作案例</h2>
<h3><a id="51_excel_52"></a>5.1 创建excel</h3>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">import</span> openpyxl
<span class="hljs-comment"># 创建一个工作簿 实例化</span>
wb = openpyxl.Workbook()
<span class="hljs-comment"># 创建一个test_case的sheet表单</span>
wb.create_sheet(<span class="hljs-string">'StudentsInfo'</span>)
<span class="hljs-comment"># 删除默认表</span>
sh = wb[<span class="hljs-string">'Sheet'</span>]
wb.remove(sh)
<span class="hljs-comment"># 保存为一个xlsx格式的文件</span>
wb.save(<span class="hljs-string">'批量导入用户模板.xlsx'</span>)
</code></div></pre>
<h3><a id="52__71"></a>5.2 读取数据</h3>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">import</span> openpyxl
</code></div></pre>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># 第一步:打开工作簿</span>
wb = openpyxl.load_workbook(<span class="hljs-string">'批量导入用户模板.xlsx'</span>)
<span class="hljs-comment"># 第二步:选取表单</span>
sh = wb[<span class="hljs-string">'StudentsInfo'</span>]
</code></div></pre>
<h4><a id="521__85"></a>5.2.1 读取指定单元格数据</h4>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># 第三步:读取指定单元格</span>
<span class="hljs-comment"># 参数 row:行 column:列</span>
ce = sh.cell(row = <span class="hljs-number">1</span>,column = <span class="hljs-number">2</span>) <span class="hljs-comment"># 读取第一行,第一列的数据</span>
<span class="hljs-built_in">print</span>(ce.value)
</code></div></pre>
<pre><div class="hljs"><code class="lang-python">输出:姓名
</code></div></pre>
<h4><a id="522__98"></a>5.2.2 按行读取数据</h4>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># 按行读取数据 list(sh.rows)</span>
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(sh.rows)[<span class="hljs-number">1</span>:]) <span class="hljs-comment"># 按行读取数据,去掉第一行的表头信息数据</span>
<span class="hljs-keyword">for</span> cases <span class="hljs-keyword">in</span> <span class="hljs-built_in">list</span>(sh.rows)[<span class="hljs-number">1</span>:]:
stu_id = cases[<span class="hljs-number">0</span>].value
stu_name = cases[<span class="hljs-number">1</span>].value
stu_sex = cases[<span class="hljs-number">2</span>].value
stu_age = cases[<span class="hljs-number">3</span>].value
<span class="hljs-built_in">print</span>(stu_id, stu_name, stu_sex, stu_age)
</code></div></pre>
<pre><div class="hljs"><code class="lang-python">输出:
[
(<Cell <span class="hljs-string">'StudentsInfo'</span>.A2>, <Cell <span class="hljs-string">'StudentsInfo'</span>.B2>, <Cell <span class="hljs-string">'StudentsInfo'</span>.C2>, <Cell <span class="hljs-string">'StudentsInfo'</span>.D2>),
(<Cell <span class="hljs-string">'StudentsInfo'</span>.A3>, <Cell <span class="hljs-string">'StudentsInfo'</span>.B3>, <Cell <span class="hljs-string">'StudentsInfo'</span>.C3>, <Cell <span class="hljs-string">'StudentsInfo'</span>.D3>),
(<Cell <span class="hljs-string">'StudentsInfo'</span>.A4>, <Cell <span class="hljs-string">'StudentsInfo'</span>.B4>, <Cell <span class="hljs-string">'StudentsInfo'</span>.C4>, <Cell <span class="hljs-string">'StudentsInfo'</span>.D4>),
(<Cell <span class="hljs-string">'StudentsInfo'</span>.A5>, <Cell <span class="hljs-string">'StudentsInfo'</span>.B5>, <Cell <span class="hljs-string">'StudentsInfo'</span>.C5>, <Cell <span class="hljs-string">'StudentsInfo'</span>.D5>)
]
<span class="hljs-number">1</span> 大佬 男 <span class="hljs-number">18</span>
<span class="hljs-number">2</span> 高手 女 <span class="hljs-number">18</span>
<span class="hljs-number">3</span> 大神 男 <span class="hljs-number">19</span>
<span class="hljs-literal">None</span> <span class="hljs-number">6</span> <span class="hljs-literal">None</span> <span class="hljs-literal">None</span>
</code></div></pre>
<h4><a id="522__json_126"></a>5.2.2 按行读取数据 结果转为json</h4>
<pre><div class="hljs"><code class="lang-python">all_row = []
<span class="hljs-keyword">for</span> cases <span class="hljs-keyword">in</span> <span class="hljs-built_in">list</span>(sh.rows)[<span class="hljs-number">1</span>:]:
row = []
stu_id = cases[<span class="hljs-number">0</span>].value
stu_name = cases[<span class="hljs-number">1</span>].value
stu_sex = cases[<span class="hljs-number">2</span>].value
stu_age = cases[<span class="hljs-number">3</span>].value
<span class="hljs-comment"># print(stu_id, stu_name, stu_sex, stu_age)</span>
data = {
<span class="hljs-string">"stu_id"</span>: stu_id,
<span class="hljs-string">"stu_name"</span>: stu_name,
<span class="hljs-string">"stu_sex"</span>: stu_sex,
<span class="hljs-string">"stu_age"</span>: stu_age
}
all_row.append(data)
<span class="hljs-built_in">print</span>(all_row)
</code></div></pre>
<pre><div class="hljs"><code class="lang-python">输出:
[
{<span class="hljs-string">'stu_id'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'stu_name'</span>: <span class="hljs-string">'大佬'</span>, <span class="hljs-string">'stu_sex'</span>: <span class="hljs-string">'男'</span>, <span class="hljs-string">'stu_age'</span>: <span class="hljs-number">18</span>},
{<span class="hljs-string">'stu_id'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'stu_name'</span>: <span class="hljs-string">'高手'</span>, <span class="hljs-string">'stu_sex'</span>: <span class="hljs-string">'女'</span>, <span class="hljs-string">'stu_age'</span>: <span class="hljs-number">18</span>},
{<span class="hljs-string">'stu_id'</span>: <span class="hljs-number">3</span>, <span class="hljs-string">'stu_name'</span>: <span class="hljs-string">'大神'</span>, <span class="hljs-string">'stu_sex'</span>: <span class="hljs-string">'男'</span>, <span class="hljs-string">'stu_age'</span>: <span class="hljs-number">19</span>},
{<span class="hljs-string">'stu_id'</span>: <span class="hljs-literal">None</span>, <span class="hljs-string">'stu_name'</span>: <span class="hljs-number">6</span>, <span class="hljs-string">'stu_sex'</span>: <span class="hljs-literal">None</span>, <span class="hljs-string">'stu_age'</span>: <span class="hljs-literal">None</span>}
]
</code></div></pre>
<h4><a id="523__158"></a>5.2.3 关闭工作薄</h4>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># 关闭工作薄</span>
wb.close()
</code></div></pre>
<h3><a id="53__165"></a>5.3 写数据</h3>
<h4><a id="531__167"></a>5.3.1 写入一行</h4>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># 第二步:选取表单</span>
sh = wb[<span class="hljs-string">'StudentsInfo'</span>]
<span class="hljs-comment"># 第一行输入</span>
sh.append([<span class="hljs-string">'1'</span>, <span class="hljs-string">'2'</span>, <span class="hljs-string">'3'</span>, <span class="hljs-string">'6'</span>])
</code></div></pre>
<h2><a id="6__177"></a>6 样式</h2>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">from</span> openpyxl.styles <span class="hljs-keyword">import</span> Font, colors, Alignment
</code></div></pre>
<h4><a id="61__sheet__183"></a>6.1 改变 sheet 标签按钮颜色</h4>
<p><a href="https://blog.csdn.net/shakespeare001/article/details/7816022" target="_blank">二进制颜色代码大全(含图)</a></p>
<pre><div class="hljs"><code class="lang-python">sh.sheet_properties.tabColor = <span class="hljs-string">"FFAA33"</span>
</code></div></pre>
<h4><a id="62__191"></a>6.2 设置单元格风格</h4>
<p>下面的代码指定了等线24号,加粗斜体,字体颜色蓝色。直接使用cell的font属性,将Font对象赋值给它。</p>
<pre><div class="hljs"><code class="lang-python">sh = wb[<span class="hljs-string">'StudentsInfo'</span>]
bold_itatic_24_font = Font(name=<span class="hljs-string">'等线'</span>, size=<span class="hljs-number">24</span>, italic=<span class="hljs-literal">True</span>, color=colors.BLUE, bold=<span class="hljs-literal">True</span>)
sh[<span class="hljs-string">'A1'</span>].font = bold_itatic_24_font
</code></div></pre>
<h4><a id="63__202"></a>6.3 对齐方式</h4>
<p>也是直接使用cell的属性aligment,这里指定垂直居中和水平居中。除了center,还可以使用right、left等等参数。</p>
<pre><div class="hljs"><code class="lang-python">sh[<span class="hljs-string">'B1'</span>].alignment = Alignment(horizontal=<span class="hljs-string">'center'</span>, vertical=<span class="hljs-string">'center'</span>)
</code></div></pre>
<h4><a id="64__210"></a>6.4 设置行高和列宽</h4>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># 第2行行高</span>
sh.row_dimensions[<span class="hljs-number">2</span>].height = <span class="hljs-number">40</span>
<span class="hljs-comment"># C列列宽</span>
sh.column_dimensions[<span class="hljs-string">'C'</span>].width = <span class="hljs-number">30</span>
</code></div></pre>
<h4><a id="65__219"></a>6.5 合并和拆分单元格</h4>
<pre><div class="hljs"><code class="lang-python">sh.merge_cells(<span class="hljs-string">'B1:G1'</span>) <span class="hljs-comment"># 合并一行中的几个单元格</span>
sh.merge_cells(<span class="hljs-string">'A3:D6'</span>) <span class="hljs-comment"># 合并一个矩形区域中的单元格</span>
</code></div></pre>
留言