使用 python 拆分 excel 文件

文章目录

1、安装虚拟环境(在特定文件夹内)

复制代码
# 头两步一台机器只执行一次即可
brew install python3
xcode-select --install

# 虚拟环境
python3 -m venv my_pandas_venv
source my_pandas_venv/bin/activate
pip install pandas
pip install openpyxl

2、脚本 split.sh

拆成 2 个

复制代码
#!/bin/bash

# 检查 Python3 和 pandas 库是否已安装
if ! command -v python3 &> /dev/null; then
    echo "需要安装 Python3。"
    exit 1
fi

if ! python3 -c "import pandas" &> /dev/null; then
    echo "需要安装 pandas 库(对于Python3)。"
    exit 1
fi

# 输入参数验证
if [ $# -ne 1 ]; then
    echo "请提供要拆分的 Excel 文件的路径作为参数。"
    exit 1
fi

input_file="$1"

# 使用 pandas 读取 Excel 文件
python3 << EOF
import pandas as pd

# 读取 Excel 文件
try:
    df = pd.read_excel("$input_file")
except Exception as e:
    print("读取 Excel 文件时发生错误:", str(e))
    exit(1)

# 获取总行数
total_rows = len(df)

# 计算每个文件应包含的行数(向上取整)
rows_per_file = -(-total_rows // 2)

# 拆分并保存到两个文件
file1 = df[:rows_per_file]
file2 = df[rows_per_file:]
try:
    file1.to_excel("output1.xlsx", index=False)
    file2.to_excel("output2.xlsx", index=False)
except Exception as e:
    print("保存拆分文件时发生错误:", str(e))
    exit(1)

print("拆分完成。")
EOF

拆成 10 个

复制代码
#!/bin/bash

# 检查 Python3 和 pandas 库是否已安装
if ! command -v python3 &> /dev/null; then
    echo "需要安装 Python3。"
    exit 1
fi

if ! python3 -c "import pandas" &> /dev/null; then
    echo "需要安装 pandas 库(对于Python3)。"
    exit 1
fi

# 输入参数验证
if [ $# -ne 1 ]; then
    echo "请提供要拆分的 Excel 文件的路径作为参数。"
    exit 1
fi

input_file="$1"

# 使用 pandas 读取 Excel 文件
python3 << EOF
import pandas as pd
import math

# 读取 Excel 文件
try:
    df = pd.read_excel("$input_file")
except Exception as e:
    print("读取 Excel 文件时发生错误:", str(e))
    exit(1)

# 获取总行数
total_rows = len(df)

# 计算每个文件应包含的行数(向上取整)
rows_per_file = math.ceil(total_rows / 10)

# 拆分成多个文件
for i in range(10):
    start_row = i * rows_per_file
    end_row = (i + 1) * rows_per_file
    file = df[start_row:end_row]
    try:
        file.to_excel(f"output{i+1}.xlsx", index=False)
    except Exception as e:
        print(f"保存拆分文件 {i+1} 时发生错误:", str(e))
        exit(1)

print("拆分完成。")
EOF

把拆分的子文件直接复制到指定子文件夹(保持子文件和原始 excel 文件同名)

复制代码
#!/bin/bash

# 检查 Python3 和 pandas 库是否已安装
if ! command -v python3 &> /dev/null; then
    echo "需要安装 Python3。"
    exit 1
fi

if ! python3 -c "import pandas" &> /dev/null; then
    echo "需要安装 pandas 库(对于Python3)。"
    exit 1
fi

input_file="$1"
output_folder="$2"

# 使用 pandas 读取 Excel 文件并拆分
python3 << EOF
import pandas as pd
import math
import os

# 读取 Excel 文件
try:
    df = pd.read_excel("$input_file")
except Exception as e:
    print("读取 Excel 文件时发生错误:", str(e))
    exit(1)

# 获取总行数
total_rows = len(df)

# 计算每个文件应包含的行数(向上取整)
rows_per_file = math.ceil(total_rows / 10)

# 拆分成多个文件
for i in range(10):
    start_row = i * rows_per_file
    end_row = (i + 1) * rows_per_file
    file = df[start_row:end_row]
    
    # 子文件夹路径
    subfolder = f"subtask{i+1}/prepareInfo"
    target_subfolder = os.path.join(subfolder)

    # 保存拆分文件到子文件夹
    output_file = os.path.join(target_subfolder, f"$input_file")
    try:
        file.to_excel(output_file, index=False)
    except Exception as e:
        print(f"保存拆分文件 {i+1} 时发生错误:", str(e))
        exit(1)

print("拆分完成。")
EOF

3、运行脚本(在特定文件夹内)

复制代码
chmod +x split.sh
source my_pandas_venv/bin/activate
(my_pandas_venv) ➜  excelSplit ./split.sh split.xlsx  

4、结果


拆分后的文件

附录

复制文件夹

复制代码
#!/bin/bash

source_dir="task"  # 源目录名
target_prefix="subtask"  # 目标目录名前缀
num_copies=10  # 复制的次数

# 创建目标目录
mkdir -p "$target_prefix"

# 复制目录
for ((i=1; i<=num_copies; i++))
do
    target_dir="${target_prefix}${i}"
    cp -r "$source_dir" "$target_dir"
done

echo "复制完成。"
相关推荐
WSSWWWSSW3 小时前
Seaborn数据可视化实战:Seaborn数据可视化基础-从内置数据集到外部数据集的应用
python·信息可视化·数据分析·matplotlib·seaborn
Small___ming3 小时前
Matplotlib 可视化大师系列(七):专属篇 - 绘制误差线、等高线与更多特殊图表
python·信息可视化·matplotlib
CodeCraft Studio5 小时前
3D文档控件Aspose.3D实用教程:使用 C# 构建 OBJ 到 U3D 转换器
开发语言·3d·c#·3d渲染·aspose·3d文件格式转换·3d sdk
superlls5 小时前
(Redis)主从哨兵模式与集群模式
java·开发语言·redis
荼蘼5 小时前
CUDA安装,pytorch库安装
人工智能·pytorch·python
杨荧6 小时前
基于Python的农作物病虫害防治网站 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python
chenglin0166 小时前
C#_gRPC
开发语言·c#
骑驴看星星a7 小时前
数学建模--Topsis(Python)
开发语言·python·学习·数学建模
学习3人组7 小时前
JupyterLab在线调试实验室
python
ygy.白茶8 小时前
线性回归入门级
人工智能·python·机器学习