Python:自动获得排名第一和最后一名的门店/代理商/分局名称并组合成邮件正文

领导觉得我发的邮件太过苍白,缺少总结,希望能像下面这样:
在这里插入图片描述
但我Excel里的内容是这样的:
在这里插入图片描述
让我一个个看?不存在的,眼睛看瞎了不说,还可能会看错。还是用Python来解决吧!(PS:其实用Excel辅助sheet用vloopup也可以解决,但是我还是喜欢Python啦~~)

from win32com.client import Dispatch
from os import path
import datetime

def excel_pre():
    '''启动excel和路径设置'''
    global xl
    xl = Dispatch("Excel.Application")
    xl.Visible = False #True是显示, False是隐藏
    xl.DisplayAlerts = 0

def today_format():
    today = datetime.date.today()
    today_format = today.strftime("%Y"+'/'+"%m"+'/'+"%d")
    return today_format

def get_last_month():
    today = datetime.date.today()
    month = int(today.strftime("%m"))
    if month == 1: return '12月'
    else: return str(month -1) + '月'

def find_name(ws, find_col, start_row, end_row, value_col, ranks):
    '''找到获得第一名或最后一名的门店/代理商/区域/渠道经理名称
    ws:表sheet名,find_col:需要匹配的列,start_row:开始的行,end_row:结束的行,value_col:门店等名字所在的列,ranks:1或0,1代表第一名,0代表最后一名
    '''
    total_num = end_row - start_row + 1
    if ranks ==0:
        ranks = total_num
    for i in range(start_row, end_row+1):
        if ws.Range(find_col + str(i)).Value == ranks:
            return ws.Range(value_col + str(i))
        else: continue

path_this_file = path.abspath('.')+"\\"
excel_pre()
excel_path = path_this_file + "2019年"+get_last_month()+"实体渠道综合评估.xlsm"

wb = xl.Workbooks.Open(excel_path)
ws_agent = wb.Sheets("代理商维度")
ws_store = wb.Sheets('门店维度')
ws_station = wb.Sheets('区域维度')
ws_manager = wb.Sheets('渠道经理维度')

first_store_jjc1 = find_name(ws_store, 'BS', 4, 14, 'C', 1) #一号竞技场第一名
last_store_jjc1 = find_name(ws_store, 'BS', 4, 14, 'C', 0) #一号竞技场最后一名
first_store_jjc2 = find_name(ws_store, 'BS', 15, 38, 'C', 1) #二号竞技场第一名
last_store_jjc2 = find_name(ws_store, 'BS', 15, 38, 'C', 0) #二号竞技场最后一名
first_store_jjc3 = find_name(ws_store, 'BS', 39, 90, 'C', 1) #三号竞技场第一名
last_store_jjc3 = find_name(ws_store, 'BS', 39, 90, 'C', 0) #三号竞技场最后一名

first_agent_big = find_name(ws_agent, 'BS', 4, 6, 'F', 1) #大型代理商第一名
last_agent_big = find_name(ws_agent, 'BS', 4, 6, 'F', 0) #大型代理商最后一名
first_agent_mid = find_name(ws_agent, 'BS', 7, 10, 'F', 1) #中型代理商第一名
last_agent_mid = find_name(ws_agent, 'BS', 7, 10, 'F', 0) #中型代理商最后一名
first_agent_small = find_name(ws_agent, 'BS', 11, 15, 'F', 1) #中型代理商第一名
last_agent_small = find_name(ws_agent, 'BS', 11, 15, 'F', 0) #中型代理商最后一名

first_manager = find_name(ws_manager, 'BO', 4, 14, 'B', 1) #渠道经理第一名
last_manager = find_name(ws_manager, 'BO', 4, 14, 'B', 0) #渠道经理最后一名

first_station = find_name(ws_station, 'BS', 4, 11, 'F', 1) #分局第一名
last_station = find_name(ws_station, 'BS', 4, 11, 'F', 0) #分局最后一名

print('{}综合评估结果如下:\n一、代理商维度\n大型代理第一名为{},最后一名为{}\n中型代理第一名为{},最后一名为{}\n小型代理第一名为{},最后一名为{}\n\n二、门店维度\n一号竞技场第一名为{},最后一名为{}\n二号竞技场第一名为{},最后一名为{}\n三号竞技场第一名为{},最后一名为{}\n\n三、区域维度\n第一名为{},最后一名为{}\n\n四、渠道经理维度\n第一名为{},最后一名为{}\n\n具体详见附件,请领先门店保持,落后门店仔细分析短板,快速优化提升。\n实体渠道运营中心\n{}'.format(get_last_month(),first_agent_big, last_agent_big, first_agent_mid, last_agent_mid, first_agent_small, last_agent_small, first_store_jjc1, last_store_jjc1, first_store_jjc2, last_store_jjc2, first_store_jjc3, last_store_jjc3, first_station, last_station, first_manager, last_manager, today_format()))
xl.quit()

这里默认第一名和最后一名只有一个,如果存在并列的情况就要复杂多了,暂时不考虑,如果以后碰到了再说吧~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值