时间:2021-03-21 python爬虫 查看: 1134
<h2>Python_Openpyxl<br>
pip install openpyxl
① 创建
```python
from openpyxl import Workbook
wb = Workbook()
ws = wb.active```
② 打开已有
python
>>> from openpyxl import load_workbook
>>> wb2 = load_workbook('文件名称.xlsx')
```python
ws['A1'] = 42
ws.append([1, 2, 3])
ws['A3'] = datetime.datetime.now().strftime("%Y-%m-%d")```
```python
>>> ws1 = wb.create_sheet("Mysheet")
>>> ws2 = wb.create_sheet("Mysheet", 0)```
```python
>>> ws3 = wb["New Title"] >>> ws4 = wb.get_sheet_by_name("New Title") >>> ws is ws3 is ws4 True```
```python
>>> print(wb.sheetnames) ['Sheet2', 'New Title', 'Sheet1']
>>> for sheet in wb: ... print(sheet.title)```
① 单一单元格访问
```python
>>> c = ws['A4']
>>> d = ws.cell(row=4, column=2, value=10)
>>> for i in range(1,101): ... for j in range(1,101): ... ws.cell(row=i, column=j)```
② 多单元格访问
```python
>>> cell_range = ws['A1':'C2']
>>> colC = ws['C'] >>> col_range = ws['C:D'] >>> row10 = ws[10] >>> row_range = ws[5:10]
>>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2): ... for cell in row: ... print(cell) <Cell Sheet1.A1> <Cell Sheet1.B1> <Cell Sheet1.C1> <Cell Sheet1.A2> <Cell Sheet1.B2> <Cell Sheet1.C2>
>>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2): ... for cell in row: ... print(cell) <Cell Sheet1.A1> <Cell Sheet1.B1> <Cell Sheet1.C1> <Cell Sheet1.A2> <Cell Sheet1.B2> <Cell Sheet1.C2>
>>> ws = wb.active >>> ws['C9'] = 'hello world' >>> tuple(ws.rows) ((<Cell Sheet.A1>, <Cell Sheet.B1>, <Cell Sheet.C1>), (<Cell Sheet.A2>, <Cell Sheet.B2>, <Cell Sheet.C2>), ... (<Cell Sheet.A8>, <Cell Sheet.B8>, <Cell Sheet.C8>), (<Cell Sheet.A9>, <Cell Sheet.B9>, <Cell Sheet.C9>))
>>> tuple(ws.columns) ((<Cell Sheet.A1>, <Cell Sheet.A2>, <Cell Sheet.A3>, ... <Cell Sheet.B7>, <Cell Sheet.B8>, <Cell Sheet.B9>), (<Cell Sheet.C1>, ... <Cell Sheet.C8>, <Cell Sheet.C9>))```
python
>>> wb.save('文件名称.xlsx')
① 改变 sheet 标签按钮颜色
python
ws.sheet_properties.tabColor = "1072BA"
② 获取最大行,最大列
```python
print(sheet.max_row) print(sheet.max_column)```
③ 获取每一行,每一列
```python
for row in sheet.rows: for cell in row: print(cell.value)
for column in sheet.columns: for cell in column: print(cell.value)```
④ 根据数字得到字母,根据字母得到数字
```python
from openpyxl.utils import get_column_letter, column_index_from_string
print(get_column_letter(2)) # B
print(column_index_from_string('D')) # 4```
⑤ 删除工作表
```python
wb.remove(sheet)
del wb[sheet]```
⑥ 矩阵置换(行 → 列)
```python
rows = [
['Number', 'data1', 'data2'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 10],
[6, 25, 5],
[7, 50, 10]]
list(zip(*rows))
[('Number', 2, 3, 4, 5, 6, 7), ('data1', 40, 40, 50, 30, 25, 50), ('data2', 30, 25, 30, 10, 5, 10)]
rows = [ ['Number', 'data1', 'data2'], [2, 40 ], # 这里少一个数据 [3, 40, 25], [4, 50, 30], [5, 30, 10], [6, 25, 5], [7, 50, 10], ]
[('Number', 2, 3, 4, 5, 6, 7), ('data1', 40, 40, 50, 30, 25, 50)]```
① 需要导入的类
python
from openpyxl.styles import Font, colors, Alignment
② 字体
下面的代码指定了等线24号,加粗斜体,字体颜色红色。直接使用cell的font属性,将Font对象赋值给它。
```python
bold_itatic_24_font = Font(name='等线', size=24, italic=True, color=colors.RED, bold=True)
sheet['A1'].font = bold_itatic_24_font```
③ 对齐方式
也是直接使用cell的属性aligment,这里指定垂直居中和水平居中。除了center,还可以使用right、left等等参数
```python
sheet['B1'].alignment = Alignment(horizontal='center', vertical='center')```
④ 设置行高和列宽
```python
sheet.row_dimensions[2].height = 40
sheet.column_dimensions['C'].width = 30```
⑤ 合并和拆分单元格
```python
sheet.merge_cells('B1:G1') # 合并一行中的几个单元格 sheet.merge_cells('A1:C3') # 合并一个矩形区域中的单元格```
python
sheet.unmerge_cells('A1:C3')
```python
import datetime
from random import choice
from time import time
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
addr = "openpyxl.xlsx"
wb = load_workbook(addr)
ws = wb.create_sheet()
ws.append(['TIME', 'TITLE', 'A-Z'])
for i in range(500): TIME = datetime.datetime.now().strftime("%H:%M:%S") TITLE = str(time()) A_Z = get_column_letter(choice(range(1, 50))) ws.append([TIME, TITLE, A_Z])
row_max = ws.max_row
con_max = ws.max_column
for j in ws.rows: # we.rows 获取每一行数据 for n in j: print(n.value, end="\t") # n.value 获取单元格的值 print()
wb.save(addr)```
以上就是python openpyxl模块的使用详解的详细内容,更多关于python openpyxl模块的资料请关注python博客其它相关文章!
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li><a href="/article/207509.htm" title="Python openpyxl 无法保存文件的解决方案" target="_blank">Python openpyxl 无法保存文件的解决方案</a></li><li><a href="/article/207485.htm" title="python openpyxl 带格式复制表格的实现" target="_blank">python openpyxl 带格式复制表格的实现</a></li><li><a href="/article/207484.htm" title="python openpyxl筛选某些列的操作" target="_blank">python openpyxl筛选某些列的操作</a></li><li><a href="/article/206571.htm" title="python中openpyxl和xlsxwriter对Excel的操作方法" target="_blank">python中openpyxl和xlsxwriter对Excel的操作方法</a></li><li><a href="/article/206407.htm" title="详解Python openpyxl库的基本应用" target="_blank">详解Python openpyxl库的基本应用</a></li><li><a href="/article/206172.htm" title="详解Python中openpyxl模块基本用法" target="_blank">详解Python中openpyxl模块基本用法</a></li><li><a href="/article/205868.htm" title="python 使用openpyxl读取excel数据" target="_blank">python 使用openpyxl读取excel数据</a></li><li><a href="/article/207510.htm" title="解决python 使用openpyxl读写大文件的坑" target="_blank">解决python 使用openpyxl读写大文件的坑</a></li></ul>