python excel怎么归类
时间: 2023-10-18 11:29:49 浏览: 95
python3.7 利用函数os pandas利用excel对文件名进行归类
使用Python对Excel进行归类的方法有多种。其中一种常见的方法是使用xlrd和xlwt库来读取和写入Excel文件。
首先,使用xlrd库打开Excel文件并获取工作表:
```python
import xlrd
data = xlrd.open_workbook('test.xlsx')
table = data.sheet_by_name('Sheet1')
```
然后,使用xlrd库的相关方法获取数据行数和列数,并打印数据:
```python
nrows = table.nrows
ncols = table.ncols
for i in range(nrows):
print(table.row_values(i))
```
另外,还可以使用openpyxl库来对Excel进行归类。下面是一个示例代码,演示了如何使用openpyxl库对Excel进行归类:
```python
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
ws.title = '汇总'
ws.append(['物料编号', '物料描述', '批号批数量'])
all_wb = openpyxl.load_workbook('日领料单.xlsx')
all_ws = all_wb.worksheets
res = {}
for w in all_ws:
for row in range(4, w.max_row-3):
if w['K' + str(row)].value not in res.keys():
res.update({w['K' + str(row)].value:{'物料描述':w['P' + str(row)].value, '批号批数量':int(w['N' + str(row)].value)}})
else:
res[w['K' + str(row)].value]['批号批数量'] = int(w['N' + str(row)].value)
li = list(res.items())
li.sort(key=lambda x:x<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Python小姿势 - Python操作Excel表格数据](https://blog.csdn.net/weixin_39032019/article/details/130356147)[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: 50%"]
- *3* [Python对excel操作——分类汇总278张Excel表中的数据](https://blog.csdn.net/qq_41404557/article/details/124754304)[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: 50%"]
[ .reference_list ]
阅读全文