Python实现Excel拆分sheet操作为文件

1.下载Python

https://www.python.org/ftp/python/3.11.4/python-3.11.4-amd64.exe

2.换个安装源(快点)

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/

2.按装依赖包

pip install pandas

pip install openpyxl

import os
import pandas as pd
# 读取Excel文件(文件要用英文名)
# excel_file = pd.ExcelFile('./'+os.listdir('./')[0])
excel_file = pd.ExcelFile('test.xlsx')
# 遍历文件中的每个sheet
for sheet_name in excel_file.sheet_names:
    # 读取sheet数据
    df = pd.read_excel(excel_file, sheet_name)
    # 保存为新的Excel文件
    df.to_excel(f'./{sheet_name}.xlsx', index=False)
    print(sheet_name,"拆分成功!")

 要文件来在同一目录(demo.py)

在目录>此电脑>demo 这个地址栏 输入

cmd

python demo.py

跟着就是等。。。

猜你喜欢

转载自blog.csdn.net/PieroPc/article/details/131338121