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

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.xdnf.cn/news/15370.html

相关文章:

  • python-enumrate函数
  • 字母异位词分组
  • Linux驱动09 --- 环境搭建
  • 计算机毕业设计Java停车场管理系统 基于Java的智能停车场管理系统开发 Java语言实现的停车场综合管理平台
  • 如何检测自动化设备中的直线导轨品质是否优良?
  • UE5多人MOBA+GAS 19、创建升龙技能,以及带力的被动,为升龙技能添加冷却和消耗
  • 【408考研知识点全面讲解计算机学科专业基础综合(408)】——数据结构之排序
  • SELECT ... INTO OUTFILE和LOAD DATA INFILE
  • 请求服务端获取broker的机房归属信息异常
  • 【C#】GraphicsPath的用法
  • ai批量抠图win和mac都可以用
  • 数据库连接池及其核心特点
  • Spring Boot整合MyBatis+MySQL+Redis单表CRUD教程
  • OneCode 3.0 DDD领域模型开放接口:基于DSMFactory的架构解析与实践指南
  • 创建 UIKit 项目教程
  • 浅谈npm,cnpm,pnpm,npx,nvm,yarn之间的区别
  • 周末总结(2024/07/12)
  • 小架构step系列12:单元测试
  • 为什么有些PDF无法复制文字?原理分析与解决方案
  • 知识宇宙-思考篇:AI大模型如何重塑软件开发流程?
  • MCP选型指南:AWS vs Azure vs GCP vs 国内云厂商深度对比
  • openGauss 的列式存储表时遇到的排序和聚合查询性能问题
  • mybatis模糊匹配采用concat与#{},动态sql讲解
  • Flutter、React Native、Uni-App 的比较与分析
  • 80. 删除有序数组中的重复项 II
  • brpc中bthread_start_urgent和tls_task_group详细机制分析
  • 使用python 实现一个http server
  • 传感器WSNs TheDataLinkLayer——X-MAC
  • 基于随机森林的金融时间序列预测系统:从数据处理到实时预测的完整流水线
  • [特殊字符] 实时数据洪流突围战:Flink+Paimon实现毫秒级分析的架构革命(附压测报告)——日均百亿级数据处理成本降低60%的工业级方案