前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python操作Excel文件(1)

Python操作Excel文件(1)

作者头像
Exploring
发布2022-09-20 14:51:51
3790
发布2022-09-20 14:51:51
举报
文章被收录于专栏:数据处理与编程实践

文章背景:Excel是Window环境下流行的、强大的电子表格应用。openpyxl模块让Python程序能够读取和修改Excel电子表格文件。下面介绍如何通过Python操作Excel文件。

本文的例子将使用一个电子表格example.xlsx。

1 读取Excel文档

(1) 打开Excel文档

代码语言:javascript
复制
import openpyxl, os
os.chdir('E:\\python123')

wb = openpyxl.load_workbook("example.xlsx")
print(type(wb))
代码语言:javascript
复制
<class 'openpyxl.workbook.workbook.Workbook'>

(2) 从工作簿中获取工作表

代码语言:javascript
复制
wb.sheetnames   # The workbook's sheets' names.
代码语言:javascript
复制
['Sheet1', 'Sheet2', 'Sheet3']
代码语言:javascript
复制
sheet = wb['Sheet3']     # Get a sheet from the workbook.
print(sheet.title)       # Get the sheet's title as a string.
代码语言:javascript
复制
Sheet3
代码语言:javascript
复制
anotherSheet = wb.active # Get the active sheet.

(3) 从表中取得单元格

代码语言:javascript
复制
sheet = wb['Sheet1']     # Get a sheet from the workbook.
sheet['A1']              # Get a cell from the sheet.
sheet['A1'].value        # Get the value from the cell.
代码语言:javascript
复制
datetime.datetime(2015, 4, 5, 13, 34, 2)
代码语言:javascript
复制
b = sheet['B1']          # Get another cell from the sheet.
b.value
代码语言:javascript
复制
'Apples'
代码语言:javascript
复制
# Get the row, column, and value from the cell.
'Row %s, Column %s is %s' % (b.row, b.column, b.value)
代码语言:javascript
复制
'Row 1, Column 2 is Apples'
代码语言:javascript
复制
'Cell %s is %s' % (b.coordinate, b.value)
代码语言:javascript
复制
'Cell B1 is Apples'
代码语言:javascript
复制
sheet.cell(row=1, column=2)
sheet.cell(row=1, column=2).value
代码语言:javascript
复制
'Apples'
代码语言:javascript
复制
sheet.max_row      # Get the highest row number.
代码语言:javascript
复制
7
代码语言:javascript
复制
sheet.max_column   # Get the highest column number.
代码语言:javascript
复制
3

(4) 列字母与数字之间的转换

代码语言:javascript
复制
import openpyxl
from openpyxl.utils import get_column_letter, column_index_from_string

get_column_letter(1)     # Translate column 1 to a letter.
代码语言:javascript
复制
'A'
代码语言:javascript
复制
column_index_from_string('A') # Get A's number.
代码语言:javascript
复制
1

(5) 从表中取得行和列

代码语言:javascript
复制
tuple(sheet['A1':'C3']) # Get all cells from A1 to C3.
代码语言:javascript
复制
((<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>))
代码语言:javascript
复制
list(sheet.columns)[1] # Get second column's cells.
代码语言:javascript
复制
(<Cell 'Sheet1'.B1>,
 <Cell 'Sheet1'.B2>,
 <Cell 'Sheet1'.B3>,
 <Cell 'Sheet1'.B4>,
 <Cell 'Sheet1'.B5>,
 <Cell 'Sheet1'.B6>,
 <Cell 'Sheet1'.B7>)

(6) 小结

  1. Import the openpyxl module.
  2. Call the openpyxl.load_workbook() function.
  3. Get a Workbook object.
  4. Use the active or sheetnames attributes.
  5. Get a Worksheet object.
  6. Use indexing or the cell() sheet method with row and column keyword arguments.
  7. Get a Cell object.
  8. Read the Cell object’s value attribute.

参考资料:

[1] Python编程快速上手—让繁琐工作自动化(https://ddz.red/AFTmO

[2] WORKING WITH EXCEL SPREADSHEETS(https://automatetheboringstuff.com/2e/chapter13/

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据处理与编程实践 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 读取Excel文档
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档