使用 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 "复制完成。"
相关推荐
csbysj202015 小时前
Python break 语句详解
开发语言
2401_8579182915 小时前
C++中的访问者模式实战
开发语言·c++·算法
格林威15 小时前
工业相机图像高速存储(C++版):RAID 0 NVMe SSD 阵列暴力提速,附海康实战代码!
开发语言·c++·人工智能·数码相机·计算机视觉·工业相机·堡盟相机
DamianGao15 小时前
MiniMax-M2.7 与 LangChain ToolStrategy 兼容性问题解决
python·langchain
elseif12315 小时前
CSP-S提高级大纲
开发语言·数据结构·c++·笔记·算法·大纲·考纲
兰.lan15 小时前
【黑马ai测试】Day01课堂笔记+课后作业
软件测试·笔记·python·ai·单元测试
开开心心就好15 小时前
绿色版PDF多功能工具,支持编辑转换
人工智能·windows·pdf·ocr·excel·语音识别·harmonyos
国医中兴15 小时前
Python AI入门:从Hello World到图像分类
人工智能·python·分类
熊猫_豆豆15 小时前
Python 基于Dlib和OpenCV实现人脸融合算法+代码
图像处理·python·算法·人脸融合
波特率11520016 小时前
C++中类的const与static关键字修饰函数与变量辨析
开发语言·c++·