python 使用openpyxl获取表数据
时间: 2023-10-16 22:00:49 浏览: 68
使用openpyxl获取表数据的步骤如下:
1. 首先,需要安装openpyxl包。可以使用以下命令进行安装:\[1\]
```
pip install openpyxl -i https://pypi.tuna.tsinghua.edu.cn/simple/
```
2. 导入openpyxl的load_workbook方法,并使用该方法打开Excel表格。\[3\]
```python
from openpyxl import load_workbook
def open_excel_data(path):
workbook = load_workbook(filename=path)
```
3. 读取表格的数据。首先获取表格的页码,然后获取标题行数据和非标题行数据,并将它们拼接成字典形式的数据。\[3\]
```python
sheet = workbook\['Sheet1'\]
title_row = \[\]
datas = \[\]
# 获取标题行数据
for row in sheet.iter_rows(max_row=1):
title_row = \[cell.value for cell in row\]
# 获取标题行外的数据,并进行拼接
for i in range(sheet.max_row-1):
for row in sheet.iter_rows(min_row=2+i,max_row=2+i):
# 非标题行的数据
row_data = \[cell.value for cell in row\]
# 拼接加入列表
datas.append(dict(zip(title_row,row_data)))
return datas
```
通过以上步骤,你可以使用openpyxl包来获取Excel表格的数据。
#### 引用[.reference_title]
- *1* *2* [python使用openpyxl处理excel表格数据](https://blog.csdn.net/qq_53568983/article/details/129300284)[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^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [python 使用openpyxl获取 Excel 数据,进行拼接返回](https://blog.csdn.net/m0_65883616/article/details/125908048)[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^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文