Python自动生成当月上班及加班时间模板表
文章目录
- 原由
- 效果图
- 表格分析
- 代码功能分析
- 代码环境
- 代码
原由
因为懒,所以每个月底都不想做当月上班及加班表格。
因为懒,所以不想每次做表格都要查日历算周末日期。
因为懒,所以想有人帮我做该表格。
因为懒,所以没有人能帮我,只能自己写代码做。
效果图
表格分析
表格标题:固定格式,宋体,14号,加粗
职员信息:固定格式,宋体,12号,加粗
A列:A5开始,当月天数列,宋体,9号
黑边框范围:A列至J列,5行开始,5+月天数+1行结束,宋体,9号
周末行:
- 如果星期天为月第一天,直接写入
- 如果星期六为月最后一天,直接写入
- 逢星期天,合并当前列格子与列前一个格子(例如:B7是星期天,合并B6:B7),写入
代码功能分析
- 默认对给定员工名,自动生成当年当月表格
- 给定员工名,指定年,月,自动生成指定年月表格
备注:详见代码最后部分,示例代码
代码环境
系统:Windows
软件:需要安装office excel(最好版本新一些,要能支持xlsx格式)
语言环境:python2.7
- 需要包:win32com.client
备注:所需包使用pip install pywin32安装
代码
# -*- coding: UTF-8 -*-# 生成当月上班及加班时间表
# by Yone 2019/12/1
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import win32com.client
import os
import datetime
import calendardef getScriptPath():return os.path.split(os.path.realpath(__file__))[0]class Date(object):def __init__(self, year=None, month=None):if year == None: year = datetime.datetime.now().yearif month == None: month = datetime.datetime.now().monthself.__year = yearself.__month = monthdef getYear(self):return self.__yeardef getMonth(self):return self.__monthdef getMonthRange(self):calendar.setfirstweekday(firstweekday=6)# eg. return value: (6, 31) 6为当月第一天,星期天(0为星期一);31为当月天数return calendar.monthrange(self.getYear(), self.getMonth())class ExcelApp(object):def __init__(self, filePath):self.__filePath = getScriptPath() + "\\" + filePath.encode('gb2312')self.__app = win32com.client.DispatchEx('Excel.Application')self.__workbook = self.__app.WorkBooks.Add()self.__sheet = self.__workbook.ActiveSheetdef getFilePath(self):return self.__filePathdef getSheet(self):return self.__sheetdef saveAndQuit(self):self.__workbook.SaveAs(self.__filePath)self.__workbook.Close()self.__app.Quit()class Table(object):def __init__(self, employee, year=None, month=None):self.__date = Date(year, month)self.__filename = '%d年上班及加班时间表.xlsx' % (self.__date.getYear())self.__app = ExcelApp(self.__filename)self.sheet = self.__app.getSheet()self.__header = '%d年%d月加班申请表' % (self.__date.getYear(), self.__date.getMonth())self.__employee = '职员:%s' % (employee)self.__metadata = {'A1:J1': self.__header,'H2': self.__employee,'A3:A4': '','B3': '考勤','B4': '时间','C3:C4': '加班内容','D3:D4': 'AM','E3:E4': 'PM','F3:F4': '批准','G3': '加班','G4': '时间','H3:J3': '累计时间','H4': '正常','I4': '周末','J4': '法定假日'}self.__auto_start_metadata = 'A5'def __writeMetaData(self):for key in self.__metadata:if ':' in key:self.sheet.Range(key).Merge()self.sheet.Range(key).Value = self.__metadata[key].encode('gb2312')self.sheet.Range(key).Font.Name = "宋体".encode('gb2312')self.sheet.Range(key).Font.Size = 9self.sheet.Range('A1:J1').Font.Name = "宋体".encode('gb2312')self.sheet.Range('A1:J1').Font.Size = 14self.sheet.Range('A1:J1').Font.Bold = Trueself.sheet.Range('H2').Font.Name = "宋体".encode('gb2312')self.sheet.Range('H2').Font.Size = 12self.sheet.Range('H2').Font.Bold = Truedef __deleteSameNameFile(self):if os.path.exists(self.__app.getFilePath()):os.remove(self.__app.getFilePath())def __writeDays(self):weekDay, days = self.__date.getMonthRange()cell1 = self.__auto_start_metadata[0:1]cell2 = self.__auto_start_metadata[1:2]for day in range(1, days + 1):self.sheet.Range(cell1 + cell2).Value = daycell2 = str(int(cell2) + 1)def __writeWeekday(self):weekDay, days = self.__date.getMonthRange()cell1 = self.__auto_start_metadata[0:1]cell2 = self.__auto_start_metadata[1:2]week = weekDaycell1 = chr(ord(cell1) + 1)for day in range(1, days + 1):if weekDay == 6 and day == 1:# B5 = 周末self.sheet.Range(cell1 + cell2).Value = '周末'.encode('gb2312')self.sheet.Range(cell1 + cell2 + ':' + 'J' + cell2).Interior.Color = self.RGB(191, 191, 191)elif weekDay == 6:# B6:B7 = 周末self.sheet.Range(cell1 + str(int(cell2) - 1) + ':' + cell1 + cell2).Merge()self.sheet.Range(cell1 + str(int(cell2) - 1) + ':' + cell1 + cell2).Value = '周末'.encode('gb2312')self.sheet.Range(cell1 + str(int(cell2) - 1) + ':' + 'J' + cell2).Interior.Color = self.RGB(191, 191, 191)# Worksheets("Sheet1").Range("A1").Interior.ColorIndex = 8 ' Cyan # objExcelSheet.cells(row,column).Interior.color = RGB(255,0,0)elif weekDay == 5 and day == days:self.sheet.Range(cell1 + cell2).Value = '周末'.encode('gb2312')self.sheet.Range(cell1 + cell2 + ':' + 'J' + cell2).Interior.Color = self.RGB(191, 191, 191)if weekDay == 6:weekDay = 0else:weekDay += 1cell2 = str(int(cell2) + 1)def RGB(self, R, G, B):return (65536 * R) + (256 * G) + (B)def __writeFinal(self):weekDay, days = self.__date.getMonthRange()cell2 = self.__auto_start_metadata[1:2]finalCell2 = str(days + int(cell2))merge = lambda c1,c2 : self.sheet.Range(c1 + finalCell2 + ':' + c2 + finalCell2).Merge()merge('A', 'C')self.sheet.Range('D' + finalCell2).Value = '统计人'.encode('gb2312')merge('E', 'F')self.sheet.Range('G' + finalCell2).Value = '小计'.encode('gb2312')def __formatCell(self):weekDay, days = self.__date.getMonthRange()A = ord('A')J = ord('J')cell2 = self.__auto_start_metadata[1:2]finalCell2 = str(days + int(cell2))# https://docs.microsoft.com/zh-cn/office/vba/api/excel.xlhalignxlHAlignCenter = -4108self.sheet.Range('A1:J' + finalCell2).HorizontalAlignment = xlHAlignCenter# https://docs.microsoft.com/zh-cn/office/vba/api/excel.xlbordersindexXlBordersIndex = {'xlDiagonalDown':5,'xlDiagonalUp':6,'xlEdgeBottom':9,'xlEdgeLeft':7,'xlEdgeRight':10,'xlEdgeTop':8,'xlInsideHorizontal':12,'xlInsideVertical':11}# https://docs.microsoft.com/zh-cn/office/vba/api/excel.xllinestyleXlLineStyle = {'xlContinuous': 1,'xlDash': -4115,'xlDashDot': 4,'xlDashDotDot': 5,'xlDot': -4118,'xlDouble': -4119,'xlLineStyleNone': -4142,'xlSlantDashDot': 13}# https://docs.microsoft.com/zh-cn/office/vba/api/excel.xlborderweightXlBorderWeight = {'xlHairline': 1,'xlMedium': -4138,'xlThick': 4,'xlThin': 2}getIndex = lambda index : self.sheet.Range('A3:J' + finalCell2).Borders(XlBordersIndex[index])getIndex('xlEdgeLeft').LineStyle = XlLineStyle['xlContinuous']getIndex('xlEdgeLeft').Weight = XlBorderWeight['xlThin']getIndex('xlEdgeTop').LineStyle = XlLineStyle['xlContinuous']getIndex('xlEdgeTop').Weight = XlBorderWeight['xlThin']getIndex('xlEdgeBottom').LineStyle = XlLineStyle['xlContinuous']getIndex('xlEdgeBottom').Weight = XlBorderWeight['xlThin']getIndex('xlEdgeRight').LineStyle = XlLineStyle['xlContinuous']getIndex('xlEdgeRight').Weight = XlBorderWeight['xlThin']getIndex('xlInsideVertical').LineStyle = XlLineStyle['xlContinuous']getIndex('xlInsideVertical').Weight = XlBorderWeight['xlThin']getIndex('xlInsideHorizontal').LineStyle = XlLineStyle['xlContinuous']getIndex('xlInsideHorizontal').Weight = XlBorderWeight['xlThin']self.sheet.Range('A3:J' + finalCell2).Font.Name = "宋体".encode('gb2312')self.sheet.Range('A3:J' + finalCell2).Font.Size = 9def createMetaDataTable(self):self.__deleteSameNameFile()self.__writeMetaData()self.__writeDays()self.__writeWeekday()self.__writeFinal()self.__formatCell()def save(self):self.__app.saveAndQuit()def test(self):print self.__filename.encode('gb2312')print self.__header.encode('gb2312')print self.__date.getYear()print self.__date.getMonth()print self.__date.getMonthRange()if __name__ == "__main__":# 以下二选一即可:名字,生成表格的年(不填,默认为当年),生成表格的月份(不填,默认为当月)# table = Table('小明', 2019, 11)table = Table('小明')table.test()table.createMetaDataTable()table.save()
Python自动生成当月上班及加班时间模板表相关推荐
- python自动生成ppt报告_把时间还给洞察,且看PPT调研报告自动生成攻略
文/JSong @2017.02.28 在数据分析里面有一句话是说,80%的时间要用于数据清洗和整理,而我觉得理想的状态应该是把更多的把时间花在数据背后的洞察当中.去年11月在简书占了个坑,说要自己写 ...
- 用Python自动生成NBA巨星生涯数据曲线
1.序 之前写过一个用 python 自动生成球员职业生涯数据的程序(原文请关注本人公众号),大家的反响很好,我也感到很欣慰.有问我怎么做的,如何学 python 的,也有提建议说集成到 web 里面 ...
- python自动汇总表格_用Python自动生成Excel报表
作者 / 来源:林骥(ID:linjiwx) 01 安装和导入模块 以 Python 中的 openpyxl 模块为例,它能够读取和修改 Excel 文件,如果你还没有安装,可以通过以下命令进行安装: ...
- python ppt自动生成_实战 | Python自动生成PPT调研报告
原标题:实战 | Python自动生成PPT调研报告 原文: 全文约 3821 字,读完可能需要 5 分钟. 文/JSong @2017.02.28 在数据分析里面有一句话是说,80%的时间要用于数据 ...
- 用Python自动生成Excel报表
在日常工作中,可能会有一些重复无聊的任务,比如说,从 Excel 或数据库中收集一些数据,设置相应的数据格式并做成报表. 类似这种重复无聊的任务,我们完全可以交给 Python 去自动完成,只要第一次 ...
- python新建word文档_使用Python 自动生成 Word 文档的教程
当然要用第三方库啦 :) 使用以下命令安装: pip install python-docx 使用该库的基本步骤为: 1.建立一个文档对象(可自动使用默认模板建立,也可以使用已有文件). 2.设置文档 ...
- python项目分析报告_实战 | Python自动生成PPT分析报告
原标题:实战 | Python自动生成PPT分析报告 在数据分析里面有一句话是说,80%的时间要用于数据清洗和整理,而我觉得理想的状态应该是把更多的把时间花在数据背后的洞察当中.去年11月在简书占了个 ...
- python制作相册_《自拍教程73》Python 自动生成相册文件夹
这里将告诉您<自拍教程73>Python 自动生成相册文件夹,具体操作过程:案例故事: 接Python mediainfo批量重命名图片文件,测试图片是批量重命名好了, 但是将测试图片放于 ...
- 用Python自动生成数据日报!
今天聊聊怎么用Python自动生成数据日报! 其实我觉得蛮简单,核心就是你组装好日报的内容模板,然后将变化的量交给python去填充,需要用到的基本就是python处理excel.word和ppt等相 ...
最新文章
- 1.SQL数据定义语言(基础)
- 并发编程-23J.U.C组件拓展之阻塞队列BlockingQueue 和 线程池
- ASP.NET 后台下载文件方法
- 微软MSDN中文网络广播(Webcast)——Visual Studio 2010 ALM应用实践系列课程预告(2011)...
- 树莓派实时(30fps)手势识别,从数据集采集开始,全部流程开源
- 数据结构之基环树——骑士,Island,旅行加强版,Number of Simple Paths,Traffic Network in Numazu,Card Game
- 《C++高级编程(第3版)》
- Java线程池(1) - 问题驱动“Java线程池”学习
- Vue v-on v-model 组合使用
- iOS中基于WebView的HTML网页离线访问技术的实现
- mysqldump备份所有数据库,恢复单个库的场景预演
- XP系统优化简单实用技法
- 三节串联锂电池充电管理芯片,IC电路图,BOM表
- 「周末观赛指南」国足生死战 NBA将演“大结局”?
- 如何复盘已搭建的会员积分系统
- leetcode No5. Longest Palindromic Substring
- 我的挣扎 与 TBtools 的开发
- openwrt 认证收费_openwrt,wifi认证-nodogsplash
- PyCenterNetDetector is not in the models registry
- JDK下载过慢的问题解决方案
热门文章
- Array.IndexOf performance caveat
- jetbrains rider 一直显示 syncing project templates
- Protein Ising Model Problem
- Google推荐系统Wide Deep Learning for Recommender Systems论文翻译解读
- 我对于UI设计这个领域的理解
- 大型网站技术架构(一)--大型网站架构演化
- 【日志】珂学——珂朵莉树
- 每日一道 LeetCode (36):相交链表
- 博客管理系统php教程,Wblog博客程序管理系统
- Android提示“很抱歉,xxx已停止运行“,adb命令连接模拟器并查看安卓日志