用openpyxl读取excel文件
时间: 2023-11-08 22:04:17 浏览: 68
Python使用openpyxl读写excel文件的方法
Sure, I'd be happy to help you with that.
To read data from an Excel workbook using Python's openpyxl module, you first need to install it using pip. Once that's done, you can use the following code snippet:
```
from openpyxl import load_workbook
# Load the workbook
workbook = load_workbook(filename='example.xlsx')
# select the sheet you want to read from
sheet = workbook.active
# iterate through the rows and columns of the sheet and print cell values
for row in sheet.iter_rows(min_row=1, min_col=1, max_row=sheet.max_row, max_col=sheet.max_column):
for cell in row:
print(cell.value)
```
This code will read all the cells in the Excel file and print their values to the console. You can modify it to read only specific cells or ranges of cells by adjusting the `min_row`, `min_col`, `max_row`, and `max_col` arguments passed to `sheet.iter_rows()`.
阅读全文