如果我想让python读取D盘Python文件夹中的Specific_data.xlsx这个文件。我应该怎么写代码
时间: 2024-10-18 07:13:26 浏览: 45
在Python中,你可以使用内置的`openpyxl`库来读取Excel文件,假设`Specific_data.xlsx`位于D盘的Python文件夹中,你可以这样做:
```python
import openpyxl
# 指定文件的完整路径
file_path = r"D:\Python\Specific_data.xlsx"
try:
# 打开Excel文件
workbook = openpyxl.load_workbook(file_path)
# 选择工作表,这里假设只有一个工作表
sheet = workbook.active
# 现在你可以遍历数据或者直接访问单元格
for row in sheet.iter_rows(values_only=True):
print(row)
except FileNotFoundError:
print("文件未找到,请检查文件路径是否正确.")
```
这段代码首先尝试打开指定的Excel文件,然后获取第一个活动的工作表(如果有多个工作表,需要先选择),最后读取并打印出每一行的数据。
阅读全文
相关推荐








