如何利用python遍历文件中的Excel文件
时间: 2023-06-02 12:06:53 浏览: 180
你可以使用Python中的xlrd库来遍历Excel文件。以下是一个示例代码:
```python
import xlrd
import os
def traverse_excel_files(rootdir):
for subdir, dirs, files in os.walk(rootdir):
for file in files:
filepath = os.path.join(subdir, file)
if filepath.endswith(".xls") or filepath.endswith(".xlsx"):
workbook = xlrd.open_workbook(filepath)
for sheet in workbook.sheets():
for row in range(sheet.nrows):
for col in range(sheet.ncols):
cell_value = sheet.cell(row, col).value
print(cell_value)
traverse_excel_files("<rootdir>")
```
在代码中,`rootdir`是你想要遍历的文件夹路径。函数会遍历该文件夹下的所有Excel文件,并打印每个单元格的值。你可以根据需要修改代码,以适应你的具体应用场景。