Python操作Excel文件(1)
2023-06-13 09:12:58 时间
文章背景:Excel是Window环境下流行的、强大的电子表格应用。openpyxl模块让Python程序能够读取和修改Excel电子表格文件。下面介绍如何通过Python操作Excel文件。
本文的例子将使用一个电子表格example.xlsx。
1 读取Excel文档
(1) 打开Excel文档
import openpyxl, os
os.chdir('E:\\python123')
wb = openpyxl.load_workbook("example.xlsx")
print(type(wb))
<class 'openpyxl.workbook.workbook.Workbook'>
(2) 从工作簿中获取工作表
wb.sheetnames # The workbook's sheets' names.
['Sheet1', 'Sheet2', 'Sheet3']
sheet = wb['Sheet3'] # Get a sheet from the workbook.
print(sheet.title) # Get the sheet's title as a string.
Sheet3
anotherSheet = wb.active # Get the active sheet.
(3) 从表中取得单元格
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.
datetime.datetime(2015, 4, 5, 13, 34, 2)
b = sheet['B1'] # Get another cell from the sheet.
b.value
'Apples'
# Get the row, column, and value from the cell.
'Row %s, Column %s is %s' % (b.row, b.column, b.value)
'Row 1, Column 2 is Apples'
'Cell %s is %s' % (b.coordinate, b.value)
'Cell B1 is Apples'
sheet.cell(row=1, column=2)
sheet.cell(row=1, column=2).value
'Apples'
sheet.max_row # Get the highest row number.
7
sheet.max_column # Get the highest column number.
3
(4) 列字母与数字之间的转换
import openpyxl
from openpyxl.utils import get_column_letter, column_index_from_string
get_column_letter(1) # Translate column 1 to a letter.
'A'
column_index_from_string('A') # Get A's number.
1
(5) 从表中取得行和列
tuple(sheet['A1':'C3']) # Get all cells from A1 to C3.
((<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>))
list(sheet.columns)[1] # Get second column's cells.
(<Cell 'Sheet1'.B1>,
<Cell 'Sheet1'.B2>,
<Cell 'Sheet1'.B3>,
<Cell 'Sheet1'.B4>,
<Cell 'Sheet1'.B5>,
<Cell 'Sheet1'.B6>,
<Cell 'Sheet1'.B7>)
(6) 小结
- Import the openpyxl module.
- Call the openpyxl.load_workbook() function.
- Get a Workbook object.
- Use the active or sheetnames attributes.
- Get a Worksheet object.
- Use indexing or the cell() sheet method with row and column keyword arguments.
- Get a Cell object.
- Read the Cell object’s value attribute.
参考资料:
[1] Python编程快速上手—让繁琐工作自动化(https://ddz.red/AFTmO)
[2] WORKING WITH EXCEL SPREADSHEETS(https://automatetheboringstuff.com/2e/chapter13/)
相关文章
- 在pycharm中如何新建Python文件?_github下载的python源码项目怎么用
- python编程是啥-Python编程「建议收藏」
- python安装不了whl文件_Python安装whl文件过程图解
- 利用Python读取和修改Excel文件(包括xls文件和xlsx文件)——基于xlrd、xlwt和openpyxl模块
- 【Python】python文件打开方式详解——a、a+、r+、w+、rb、rt区别[通俗易懂]
- python实现将数据写入Excel文件中「建议收藏」
- 怎么用python打开csv文件_Python文本处理之csv-csv文件怎么打开[通俗易懂]
- 【说站】python异常处理的作用
- Python实现自动回复_python 微信机器人
- python pkl文件_Python字符串格式化输出的方式包括
- python attrs_Python attrs作用是什么?
- python判断文件后缀_Python 判断文件后缀是否被篡改
- python读取excel文件代码_python怎么加速读取excel
- 正则表达式Python_python正则表达式匹配字符串
- 高质量编码--Python提取与合并Excel数据
- python-Python与PostgreSQL数据库-使用Python执行PostgreSQL查询(二)
- MySQL导入Excel:一步步实现数据导入(mysql导入excel)
- Python在MSSQL中的应用实践(python与mssql)
- Python与MySQL实现数据分析的完美组合(mysql中python)