当前位置: 首页 > news >正文

python excel处理

xlrd

xlrd只能读取内容。

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

import xlrdbook = 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 

from openpyxl import load_workbookbook = 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:B2print(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)

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

 

调色网站

CSS Color Codes 

 

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

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

 

http://www.dtcms.com/a/275303.html

相关文章:

  • 实用技巧 Excel 与 XML互转
  • Go 设计模式 - 组合复用
  • 校园幸运抽(抽奖系统)测试报告
  • Agent 设计模式
  • 大模型KV缓存量化误差补偿机制:提升推理效率的关键技术
  • 【设计模式】外观模式(门面模式)
  • pdf合并
  • Git系列--4.Git分支设计规范
  • 计算机视觉 之 经典模型汇总
  • QT跨平台应用程序开发框架(6)—— 常用显示类控件
  • 【电脑】硬盘驱动器(HDD)的基础知识
  • Qt 多线程编程:单例任务队列的设计与实现
  • Spring Ai Alibaba Gateway 实现存量应用转 MCP 工具
  • 力扣 hot100 Day42
  • AI 时代的分布式多模态数据处理实践:我的 ODPS 实践之旅、思考与展望
  • RabbitMQ面试精讲 Day 1:RabbitMQ核心概念与架构设计
  • ntfs - SELinux
  • Vue框架之钩子函数详解
  • 路由双向引入实验:将一种路由协议学习到的路由注入另一种协议中
  • Linux中的git命令
  • Spring Cloud Gateway中常见的过滤器
  • 【kubernetes】--controller(DaemonSet)
  • Git入门教程
  • 【离线数仓项目】——电商域DIM层开发实战
  • 【一起来学AI大模型】RAG系统流程:查询→向量化→检索→生成
  • 医疗AI前端开发中的常见问题分析和解决方法
  • OpenCL study - code02
  • 箭头函数(Arrow Functions)和普通函数(Regular Functions)
  • 7. 负载均衡:流量调度引擎
  • 8-day06预训练模型