前言:
虽然大家一直诟病钉钉,但不可否认钉钉这个软件本身还是很强大的,日报也是与微信相比比较突出的一个功能。通过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