win python 读入excel
时间: 2023-10-12 20:06:17 浏览: 111
在Win Python中,可以使用不同的库来读取Excel文件。其中一种常用的库是openpyxl。它支持读取Excel 2007及以上版本的文件。你可以使用以下代码来读取Excel文件:
```python
import openpyxl
def read_excel(excel_name):
workbook = openpyxl.load_workbook(excel_name)
# 读取sheet
sheet = workbook.active
# 遍历行
for row in sheet.iter_rows():
# 遍历单元格
for cell in row:
print(cell.value)
# 调用函数读取Excel
read_excel("testexcel2007.xlsx")
```
除了openpyxl,还有其他库可以在Win Python中读取Excel文件,比如xlrd。使用xlrd的例子代码如下:
```python
import xlrd
def read_excel(excel_name):
workbook = xlrd.open_workbook(excel_name)
# 读取sheet
sheet = workbook.sheet_by_index(0) # 假设要读取第一个sheet
# 遍历行
for row in range(sheet.nrows):
# 遍历列
for col in range(sheet.ncols):
print(sheet.cell_value(row, col))
# 调用函数读取Excel
read_excel("testexcel2003.xls")
```
请根据你的需要选择适合的库来读取Excel文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Python开发学习之Python和Excel的数据实现互通](https://blog.csdn.net/xuezhangmen/article/details/129517595)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文