利用python将excel表拆分为多页sheet的多张excel

任务目标:根据汇总表数据,按照各单位区域划分,拆成各区域模块下的多个sheet镇街excel表

(说明:所有数据均做清洗或者作假,防止追究责任)

任务图片:在这里插入图片描述
效果图片:在这里插入图片描述
附上代码
#!/usr/bin/python
# -*- coding:utf-8 -*-

import pandas as pd
import os


# 一、拆分客户汇总表
path = r'C:\**\客户汇总表0321.xlsx'
data = pd.read_excel(path, sheet_name=u'汇总表', skiprows=1)
rows = data.shape[0]
danwei_list = list()

for i in range(rows):
    temp = data[u'单位'][i]
    if temp not in danwei_list:
        danwei_list.append(temp)
        
for danwei in danwei_list:
    new_df = pd.DataFrame()
    for i in range(0, rows):
        if data[u'单位'][i] == danwei:
            new_df = pd.concat([new_df, data.iloc[[i], :]], axis=0, ignore_index=True)
    new_df.to_excel(str(danwei)+'.xls', sheet_name=u'单位汇总', index=False)

# 二、拆分各单位报表,按镇区分为多个sheet表
xlsx_names = [x for x in os.listdir() if x.endswith('.xls')]
for xlsx_name in xlsx_names:
    ndata = pd.read_excel(xlsx_name)
    nrows = ndata.shape[0]

    zhenqu_list = []
    for i in range(nrows):
        temp = ndata[u'镇区'][i]
        if temp not in zhenqu_list:
            zhenqu_list.append(temp)

    n = len(zhenqu_list)
    writer = pd.ExcelWriter(xlsx_name)
    for zhenqu in range(n):
        df = pd.DataFrame()
        for i in range(0, nrows):
            if ndata[u'镇区'][i] == zhenqu_list[zhenqu]:
                df = pd.concat([df, ndata.iloc[[i], :]], axis=0, ignore_index=True)
        df.to_excel(writer, sheet_name=str(zhenqu_list[zhenqu]), encoding="utf-8", index=False)
    writer.save()
    writer.close()    

参考链接:利用Python将一张Excel文件的sheet拆分成多个sheets

  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

广西小表哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值