python与excel
时间: 2023-09-13 18:05:51 浏览: 177
Python可以很方便地操作Excel文件,使用第三方库openpyxl可以读取、修改和创建Excel文件。
以下是一个使用openpyxl读取Excel文件并输出每个单元格内容的示例代码:
```python
import openpyxl
# 打开Excel文件
wb = openpyxl.load_workbook('example.xlsx')
# 打开工作表
sheet = wb.active
# 遍历每个单元格并输出内容
for row in sheet.iter_rows():
for cell in row:
print(cell.value)
```
同样,Python也可以使用pandas库来处理Excel文件,pandas提供了类似于SQL的数据操作方式,可以对Excel文件进行高效的数据处理和分析。以下是一个使用pandas读取Excel文件并输出每个单元格内容的示例代码:
```python
import pandas as pd
# 读取Excel文件
df = pd.read_excel('example.xlsx')
# 输出每个单元格的内容
for index, row in df.iterrows():
for col in df.columns:
print(row[col])
```
无论是使用openpyxl还是pandas,都可以很方便地操作Excel文件。
阅读全文
相关推荐


















