赞
踩
brew install python3
xcode-select --install
python3 -m venv my_pandas_venv
source my_pandas_venv/bin/activate
pip install pandas
pip install openpyxl
#!/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
chmod +x split.sh
source my_pandas_venv/bin/activate
(my_pandas_venv) ➜ excelSplit ./split.sh split.xlsx
拆分后的文件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。