分割excel文件为多个
import pandas as pd
import math
from openpyxl import load_workbook
#修改A1单元格
def modify_cell(filename):
wb = load_workbook(filename=filename)
ce = wb[‘Sheet1‘][‘A1‘]
if ce.value is None:
ce = "登记编号"
else:
print("-----------------------------------------")
print("请打开文件检查!")
wb.save(filename=filename)
#分割文件
def splitTofile(df,filenum):
rows,cols = df.shape #获取行数,列数,默认第一列表表头不算行数
split_num = math.ceil(rows/filenum) #指定分隔行数
value = (filenum if rows%4 == 0 else filenum - 1) #标准分割次数
rows_format = value*split_num #标准分割所占用的总行数
#存储列表的列表
new_list = [[i,i+split_num] for i in range(1,rows_format,split_num)]
#标准行数文件
for i_j in new_list:
i,j = i_j
excel_small = df[i:j]
#index为False,否则就把行索引写入了
excel_small.to_excel("C:/Users/silent_426/Desktop/爬状态/{0}_{1}.xlsx".format(i,j),index=False)
if rows%4 != 0:
#最后分割出的文件
df[rows_format:].to_excel("C:/Users/silent_426/Desktop/爬状态/last.xlsx",index=False)
if __name__ == ‘__main__‘:
filename = "C:/Users/silent_426/Desktop/工作簿1.xlsx"
modify_cell(filename)
df = pd.read_excel(filename)
#分割文件个数
filenum = 4
splitTofile(df,filenum)
原文:https://www.cnblogs.com/haoLee50/p/14874649.html