Pandas:将钉钉的日报清单进行提取和整理

前言:

虽然大家一直诟病钉钉,但不可否认钉钉这个软件本身还是很强大的,日报也是与微信相比比较突出的一个功能。通过PC端钉钉,可以把一段时间内的日报批量导出,但是因为是清单格式,所以看起来并不是特别方便,所以我就想把它改成类似于下面的形式:

姓名3月1日3月2日3月3日
张三未交日报内容1日报内容2
李四日报内容3日报内容4未交

本来以为会很复杂,但是Pandas里,使用aggfunc=‘max’,就可以对字符串进行全部显示了,非常有用(mean不行),结合pivot_table即可整理成上面的样子。

Python的代码:

import pandas as pd
from win32com.client import Dispatch
import time
import os

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

def useVBA(file_path, VBA):
    '''运行宏'''
    xlBook = xl.Workbooks.Open(file_path, False)
    time.sleep(3)
    xlBook.Application.Run(VBA)
    print(">>>宏:{}已运行".format(VBA))
    xlBook.Save()
    print(">>>{}已保存".format(file_path))
    time.sleep(3)
    xlBook.Close()

#将导出的日报清单进行提取和整理
for i in os.listdir('.'):
    if i.startswith('日志报表'):
        daily_report = i
        break
dd_xls = pd.read_excel(daily_report)
dd_xls['填报日期'] = dd_xls['填报时间'].apply(lambda x: x[5:11])
dd_pivot = dd_xls.pivot_table(index='填报人', values='今日完成工作', columns='填报日期', aggfunc='max', fill_value='今日未提交').reset_index()
date_list = list(dd_xls['填报日期'].unique())
date_period = date_list[0] + '-' + date_list[-1] #新的sheet名

#导出到"技术研发中心日报整理.xlsx"中
dd_pivot.to_excel('技术研发中心日报汇总.xlsx', index=False)

#把格式进行变换
excel_pre()
useVBA(r'C:\Users\Administrator\Desktop\钉钉日报整理\日报汇总宏.xlsm', '数据格式调整')

#把日报汇总改名
os.rename('技术研发中心日报汇总.xlsx', '日报汇总'+ date_period +'.xlsx')
print('程序全部运行完成!')

VBA部分的代码:

Sub 数据格式调整()
'
' 宏1 宏
'
'运行时禁止提示
Application.DisplayAlerts = False

Path = Application.ThisWorkbook.Path
Path_ribao = Path & "\技术研发中心日报汇总.xlsx"
Workbooks.Open (Path_ribao)
    

    Columns("B:F").Select
    Selection.ColumnWidth = 31#
    Rows("2:14").Select
    Selection.RowHeight = 70
    Cells.Select
    With Selection
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""今日未提交"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True
    
    '把日报保存
    ActiveWorkbook.Save
    ActiveWindow.Close

End Sub

成果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值