# 用openpyxl遍历excel表格

2019年4月4日 / 768次阅读
Python

Python的openpyxl模块，提供了对excel表格的读写操作接口。本文介绍使用openpyxl来遍历excel表格。

openpyxl支持Excel 2010 xlsx/xlsm/xltx/xltm files，官网：https://openpyxl.readthedocs.io

openpyxl帮助我们解决了这个问题，看代码：

from openpyxl import load_workbook
ws = wb['big_data']

for row in ws.rows:
for cell in row:
print(cell.value)

Worksheet dimensions

Read-only mode relies on applications and libraries that created the file providing correct information about the worksheets, specifically the used part of it, known as the dimensions. Some applications set this incorrectly. You can check the apparent dimensions of a worksheet using ws.calculate_dimension(). If this returns a range that you know is incorrect, say A1:A1 then simply resetting the max_row and max_column attributes should allow you to work with the file:

ws.reset_dimensions()

Ctrl+D 收藏本页