目标
需要的工具包
# 导入工具包pandas
import pandas as pd
# 定义函数,输入文件路径参数和分批的单位
def export_table_in_batches(file_path, batch_size):
df = pd.read_excel(file_path) # 读取表格数据
num_rows = df.shape[0] # 获取表格的行数
num_batches = (num_rows + batch_size - 1) // batch_size # 计算需要分成多少批次
for i in range(num_batches):
start_index = i * batch_size # 计算当前批次的起始索引
end_index = min((i + 1) * batch_size, num_rows) # 计算当前批次的结束索引
batch_df = df[start_index:end_index] # 获取当前批次的数据
batch_file_path = f"{file_path}_{i+1}.xlsx" # 构造当前批次的文件路径
batch_df.to_excel(batch_file_path, index=False) # 将当前批次的数据导出到文件中
# 执行函数,打开文件夹目录下的template.xlsx文件,按每500条分批表格导出。
export_table_in_batches("template.xlsx", 500)