使用 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 "复制完成。"
相关推荐
该用户已不存在3 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP5 小时前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户83562907805110 小时前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
侃侃_天下10 小时前
最终的信号类
开发语言·c++·算法
c8i10 小时前
python中类的基本结构、特殊属性于MRO理解
python
echoarts11 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
liwulin050611 小时前
【ESP32-CAM】HELLO WORLD
python
Aomnitrix11 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
Doris_202311 小时前
Python条件判断语句 if、elif 、else
前端·后端·python
Doris_202311 小时前
Python 模式匹配match case
前端·后端·python