当前位置: 首页 > 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.lryc.cn/news/586204.html

相关文章:

  • 【世纪龙科技】新能源汽车结构原理体感教学软件-比亚迪E5
  • Windows 用户账户控制(UAC)绕过漏洞
  • 单细胞分析教程 | (二)标准化、特征选择、降为、聚类及可视化
  • 力扣-24.两两交换链表中的节点
  • 7. 负载均衡:流量调度引擎
  • STM32--USART串口通信的应用(第一节串口通信的概念)
  • stack和queue的使用和模拟实现以及了解deque
  • Kafka——聊聊Kafka的版本号
  • React 组件中怎么做事件代理?它的原理是什么?
  • 【6.1.0 漫画数据库技术选型】
  • LRU缓存机制完全实战:链表的工程落地与面试通关
  • 复现永恒之蓝
  • 网络配置综合实验全攻略(对之前学习的总结)
  • 脉冲神经网络膜电位泄漏系数学习:开启时空动态特征提取的新篇章
  • docker配置
  • 【android bluetooth 协议分析 07】【SDP详解 2】【SDP 初始化】
  • 使用FastAdmin框架开发二
  • 算法魅力-BFS解决最短路问题
  • 【Elasticsearch】function_score与rescore
  • MS Azure Eventhub 发送 AD log 到cribl
  • 定长子串中元音的最大数目
  • PyQt5布局管理(QBoxLayout(框布局))
  • OSPFv3-一二类LSA
  • Java 之字符串 --- String 类
  • 机器学习(ML)、深度学习(DL)、强化学习(RL)关系和区别
  • 箭头函数(Arrow Functions)和普通函数(Regular Functions)
  • 虚拟现实的镜廊:当技术成为存在之茧
  • 云端docker小知识
  • Java 大视界:基于 Java 的大数据可视化在智慧城市能源消耗动态监测与优化决策中的应用(2025 实战全景)
  • YOLO家族内战!v5/v8/v10谁才是你的真命天子?(附保姆级选择指南)