python excel处理

xlrd

xlrd只能读取内容。

可以看出,数字以小数的形式返回了。常规和数字格式下,保留小数点后一位。设置完格式之后,双击单元格才能生效。如果单元格内没有内容,设置完格式就生效了,此时往单元格内写内容就是设置的格式了。

python 复制代码
import xlrd

book = xlrd.open_workbook("income.xlsx")

sheet = book.sheet_by_index(1)

# 行号、列号都是从0开始计算
for i in range(0, 4):
    row = sheet.row_values(rowx=i)
    print(f"第{i + 1}行内容是: {row}")
    for j in row:
        print(j, type(j))
"""
第1行内容是: ['常规', '数字', '文本']
常规 <class 'str'>
数字 <class 'str'>
文本 <class 'str'>
第2行内容是: [123.0, 456.0, '789']
123.0 <class 'float'>
456.0 <class 'float'>
789 <class 'str'>
第3行内容是: [123.0, 456.0, '456.0']
123.0 <class 'float'>
456.0 <class 'float'>
456.0 <class 'str'>
第4行内容是: [123.01, 456.01, '456.01']
123.01 <class 'float'>
456.01 <class 'float'>
456.01 <class 'str'>
"""

sheet.row_values(rowx=0) 还可以指定开始列和结束列的位置。

sheet.col_values(colx=0) 可以指定开始行结束行的位置

openpyxl

python 复制代码
from openpyxl import load_workbook

book = load_workbook(filename='income.xlsx')

ws = book['Sheet1']

# 方法一
# 获取A2这个单元格
cell_A2 = ws['A2']
print(cell_A2) # <Cell 'Sheet1'.A2>
# 方法二:row 行;column 列
# 获取B2这个单元格
cell_B2 = ws.cell(row=2, column=2)
# 通过切片
cell_area = ws['A1':'B4']
print(cell_area)
"""
每一行的内容返回一个元组,最终是元组构成的元组
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>), (<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>),
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>), (<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>))
"""
cell_exact = ws.iter_rows(min_row=1, max_row=2, min_col=1, max_col=2)     #即A1:B2
print(cell_exact) # <generator object Worksheet._cells_by_row at 0x0000021965587900>
for i in cell_exact:
    print(i)
"""
每一行构成的元组
(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>)
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>)
"""
cell_exact1 = ws.iter_rows(min_row=1, max_row=1, min_col=1, max_col=2)     #即A1:B1
print(cell_exact1) # <generator object Worksheet._cells_by_row at 0x0000013CF0D9BCF0>
print(next(cell_exact1))
"""
第一行元素构成的元组
(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>)
"""
cell_exact2 = ws.iter_cols(max_col=2, max_row=2)
print(cell_exact2)  # <generator object Worksheet._cells_by_col at 0x000001F599352C10>
for col in cell_exact2:  #即A1:B2
    print(col)
"""
(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>)
(<Cell 'Sheet1'.B1>, <Cell 'Sheet1'.B2>)
"""
# 通过行/列
col_A = ws['A']  # A列
print(col_A)
"""
列单元格构成的元组
(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>,
<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.A4>,
<Cell 'Sheet1'.A5>, <Cell 'Sheet1'.A6>)
"""
col_area = ws['A:B']  # A、B列
"""
A列是一个元组,B列是一个元组
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>,
<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.A5>, <Cell 'Sheet1'.A6>),
(<Cell 'Sheet1'.B1>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.B3>,
<Cell 'Sheet1'.B4>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.B6>))
"""
print(col_area)
row_2 = ws[2]  # 第2行
print(row_2)
# (<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>)
row_area = ws[2:3]  # 2-3行
print(row_area)
"""
每一个单元格构成一个元组
((<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>),
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>))
"""
# 迭代所有行
all_by_row = ws.rows
print(all_by_row)
# <generator object Worksheet._cells_by_row at 0x00000213573C46D0>
print(list(all_by_row))
"""
[(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>),
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>),
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>),
(<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.C4>),
(<Cell 'Sheet1'.A5>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.C5>),
(<Cell 'Sheet1'.A6>, <Cell 'Sheet1'.B6>, <Cell 'Sheet1'.C6>)]
"""
"""
Python 中的生成器(如 ws.rows, ws.columns)就像一个"流水线",
每次读取就前进一格。读完一次后,这个生成器就"空"了,不能再次使用。
生成器只能遍历一次 所以使用tuple的时候需要在生成一次
"""
all_by_row2 = ws.rows
print(tuple(all_by_row2))
"""
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>),
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>),
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>),
(<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.C4>),
(<Cell 'Sheet1'.A5>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.C5>),
(<Cell 'Sheet1'.A6>, <Cell 'Sheet1'.B6>, <Cell 'Sheet1'.C6>))
"""
# 迭代所有列
all_by_col = ws.columns
print(all_by_col)
# <generator object Worksheet._cells_by_col at 0x00000213573E4430>
print(list(all_by_col))
"""
[(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>,
<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.A5>, <Cell 'Sheet1'.A6>),
(<Cell 'Sheet1'.B1>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.B3>,
<Cell 'Sheet1'.B4>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.B6>),
(<Cell 'Sheet1'.C1>, <Cell 'Sheet1'.C2>, <Cell 'Sheet1'.C3>,
<Cell 'Sheet1'.C4>, <Cell 'Sheet1'.C5>, <Cell 'Sheet1'.C6>)]
"""
all_by_col2 = ws.columns
print(tuple(all_by_col2))
"""
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>,
<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.A5>, <Cell 'Sheet1'.A6>),
(<Cell 'Sheet1'.B1>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.B3>,
<Cell 'Sheet1'.B4>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.B6>),
(<Cell 'Sheet1'.C1>, <Cell 'Sheet1'.C2>, <Cell 'Sheet1'.C3>,
<Cell 'Sheet1'.C4>, <Cell 'Sheet1'.C5>, <Cell 'Sheet1'.C6>))
"""
print(ws.max_row, ws.max_column)
# 6 3 第六行虽然没数据 但是我设置了单元格格式。
#------
# row_dimensions 行的属性 column_dimensions 列的属性

# 如果你只想要工作薄的值,
# 你可以使用 Worksheet.values 属性。
# 这会遍历工作簿中所有的行但只返回单元格值:
print(ws.values) # <generator object Worksheet.values at 0x00000286C5054C10>
print(list(ws.values))
"""
[('常规', '数字', '文本'), (123, 456, '789'),
(123, 456, '456.0'), (123.01, 456.01, '456.01'),
(123, 456, '456.00'), (None, None, None)]
"""
# Worksheet.iter_rows 和 Worksheet.iter_cols 可以用 values_only 参数来返回单元格值:
cell_areav = ws.iter_rows(min_row=1, max_row=2,
                          min_col=1, max_col=2, values_only=True) #即A1:B2
print(cell_areav)
# <generator object Worksheet._cells_by_row at 0x00000256DF983BA0>
print(list(cell_areav))
# [('常规', '数字'), (123, 456)]

给第一个单元格写入内容

sheet['A1'] = '你好'

可以直接给单元格赋值。

在第2行的位置插入1行 sheet.insert_rows(2)

在第3行的位置插入3行 sheet.insert_rows(3,3)

在第3行的位置删除3行 sheet.delete_rows(3,3)

python 复制代码
# 指定单元格字体颜色,
sheet['A1'].font = Font(color=colors.RED, #使用预置的颜色常量
                        size=15,    # 设定文字大小
                        bold=True,  # 设定为粗体
                        italic=True # 设定为斜体
                        )

调色网站

CSS Color Codes

python 复制代码
# 指定整行 字体风格, 这里指定的是第3行
font = Font(color="981818")
for y in range(1, 100): # 第 1 到 100 列
    sheet.cell(row=3, column=y).font = font

图片的左上角和d2的左上角重合

相关推荐
魔力之心几秒前
sklearn study notes[1]
人工智能·python·sklearn
云空30 分钟前
《PyQt6-3D:开启Python 3D开发新世界》
python·3d·pyqt
Q_Q196328847531 分钟前
python的平安驾校管理系统
开发语言·spring boot·python·django·flask·node.js·php
白毛大侠34 分钟前
在 Ubuntu 24.04 中安装 Python 2.7、pip 及 mysqlclient==1.4.6 的完整指南
python·ubuntu·pip
一百天成为python专家41 分钟前
python正则表达式(小白五分钟从入门到精通)
数据库·python·正则表达式·pycharm·python3.11
捉鸭子2 小时前
转转APP逆向
爬虫·python·网络安全·网络爬虫
芷栀夏3 小时前
飞算Java AI开发助手:引领智能编程新风尚
java·人工智能·python
对你无可奈何3 小时前
ubuntu 22.04 anaconda comfyui的安装
python·ubuntu·aigc
站大爷IP3 小时前
Python深浅拷贝全解析:从原理到实战的避坑指南
python
wh_xia_jun3 小时前
Python 数据挖掘之数据探索
python