def alipay2note(): cnxp = lite.connect(dbpathdingdanmingxi) pathalipay = dirmainpath / 'data' / 'finance' / 'alipay' dfall = chulidataindir(cnxp, 'alipay', '支付宝流水', '2088802968197536', '支付宝', pathalipay, chulixls_zhifubao) zhds = fenliu2note(dfall) cnxp.close() financesection = '财务流水账' item = '支付宝白晔峰流水条目' cfpzysm, inizysmpath = getcfp('everzysm') if not cfpzysm.has_option(financesection, item): count = 0 else: count = cfpzysm.getint(financesection, item) if count == zhds.shape[0]: log.info(f'{item}\t{zhds.shape[0]}\t无内容更新。') return zhds else: log.info(f'{item}\t{zhds.shape[0]}\t内容有更新。') nowstr = datetime.datetime.now().strftime('%F %T') imglist2note( get_notestore(), [], 'f5bad0ca-d7e4-4148-99ac-d3472f1c8d80', f'支付宝白晔峰流水({nowstr})', tablehtml2evernote(zhds, tabeltitle='支付宝白晔峰流水', withindex=False)) cfpzysm.set(financesection, item, f'{zhds.shape[0]}') cfpzysm.write(open(inizysmpath, 'w', encoding='utf-8')) return zhds
def notification2df(items): split_items = list() for itemstr in items: split_items.append(itemstr.strip().split('||| ')) dfnoti = pd.DataFrame(split_items, columns=('atime', 'shuxing', 'topic', 'content')) dfnoti['received'] = True # global log log.info('系统提醒记录有%d条。' % dfnoti.shape[0]) # descdb(dfnoti) dfnoti.drop_duplicates(inplace=True) log.info('系统提醒记录去重后有%d条。' % dfnoti.shape[0]) dfnoti.index = dfnoti['atime'].apply(lambda x: pd.to_datetime( datetime.datetime.strptime(x.strip(), '%B %d, %Y at %I:%M%p')) if len( x.split('at')) > 1 else pd.to_datetime( datetime.datetime.strptime(x.strip(), '%B %d, %Y'))) # dfnoti.index = dfnoti['atime'] del dfnoti['atime'] dfnoti.sort_index(ascending=False, inplace=True) # descdb(dfnoti) dfout = dfnoti # b3a3e458-f05b-424d-8ec1-604a3e916724 try: notestore = get_notestore() xiangmu = ['微信', '支付宝', 'QQ', '日历'] cfplife, inilifepath = getcfp('everlife') for xm in xiangmu: biaoti = '系统提醒(%s)记录' % xm dfxm = dfnoti[dfnoti.shuxing == xm] if cfplife.has_option('lifenotecount', xm): ready2update = dfxm.shape[0] > cfplife.getint( 'lifenotecount', xm) else: ready2update = True print('%d\t%s\t%s' % (dfxm.shape[0], ready2update, biaoti)) if ready2update: imglist2note(notestore, [], cfplife.get('notesguid', xm), biaoti, tablehtml2evernote(dfxm[:1000], biaoti)) cfplife.set('lifenotecount', xm, '%d' % dfxm.shape[0]) cfplife.write(open(inilifepath, 'w', encoding='utf-8')) except Exception as ee: log.critical('更新系统提醒笔记时出现错误。%s' % str(ee)) return dfout
def callsms2df(itemstr): # 读取老记录 # global dirmainpath with open(str(dirmainpath / 'data' / 'ifttt' / 'smslog_gmail_all.txt'), 'r', encoding='utf-8') as fsms: items = [line.strip() for line in fsms if len(line.strip()) > 0] itemstr = itemstr + items log.info('电话短信记录有%d条。' % len(itemstr)) itemstrjoin = '\n'.join(itemstr) pattern = re.compile( r'^(.*?)(?:(\w+ \d+, \d{4} at \d{2}:\d{2}[A|P]M)|(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\+08:00))(.*?)$', re.U | re.M) slices = re.split(pattern, itemstrjoin) slices = slices[1:] # print(len(slices)) # for i in range(15): # print("%d\t%s" %(i,slices[i])) # pattern = re.compile(r'^(?:\w*?)(?:\s*?)@SMS \+(?:\s+)(\w+ \d+, \d{4} at \d{2}:\d{2}[A|P]M)(?:\s*?),' # r'(.*?)(?:\s+)(.*?)(?:\s+)([\d|-]+)(?:.*?)(?:\s+)(\d+)(?:\s+)(?:.*?)$', re.U|re.M) # slices = re.split(pattern, itemstr) # print(len(slices)) # for i in range(30): # print(slices[i].strip()) # split_items = list() for i in range(int(len(slices) / 5)): item = list() # print(slices[i*5:i*5+5]) itemtimestr = slices[i * 5 + 1] if itemtimestr is None: itemtimestr = slices[i * 5 + 2].strip() itemtime = datetime.datetime.strptime(itemtimestr, '%Y-%m-%d %H:%M:%S+08:00') # itemtime = pd.to_datetime(itemtimestr) else: itemtime = datetime.datetime.strptime(slices[i * 5 + 1].strip(), '%B %d, %Y at %I:%M%p') itemname = '' itemnumber = '' itemshuxing = '电话' itemreceived = True itemcontentstr = slices[i * 5 + 3].strip() itemcontent = itemcontentstr patternsms = re.compile(r':(.*),(\w*)\|\|\| (\d+)', re.U) splititem = re.split(patternsms, itemcontentstr) if len(splititem) == 5: # :抱歉 明天见,||| 13554015554 # ['', '抱歉 明天见', '', '13554015554', ''] itemcontent = splititem[1] itemname = splititem[2] itemnumber = splititem[3] itemshuxing = '短信' if len(itemname) == 0: itemnamestr = slices[i * 5 + 0].strip() if itemnamestr.endswith('@SMS +'): [itemname, *__] = itemnamestr.split('@SMS +') patterncall7 = re.compile( '^,?(?:(电话打给)|(电话来自))\s+(?:(.+?)\s+)?(\d+),时长\s+(\d+)\s+秒$', re.U) splititem = re.split(patterncall7, itemcontentstr) # print(len(splititem)) # splititem = itemcontentstr.split() if len(splititem) == 7: # ,电话打给 陈益 15623759390,时长 42 秒 # ['', '电话打给', None, '陈益', '15623759390', '42', ''] # ,电话来自 18671454792,时长 29 秒 # ['', None, '电话来自', None, '18671454792', '29', ''] # ,电话打给 黄兆钢 15337282888,时长 42 秒 # ['', '电话打给', None, '黄兆钢', '15337282888', '42', ''] # ,电话来自 白天军 13949452849,时长 35 秒 # ['', None, '电话来自', '白天军', '13949452849', '35', ''] itemshuxing = '电话' itemname = splititem[3] itemnumber = splititem[4] itemcontent = splititem[5] if splititem[1]: itemreceived = False # else: patterncall5 = re.compile('(错过来电)\s+(?:(.+?)\s+)?(\d+)', re.U) splititem = re.split(patterncall5, itemcontentstr) if len(splititem) == 5: # 错过来电 曹树强 13971167977 # ['', '错过来电', '曹树强', '13971167977', ''] # 错过来电 18903752832 # ['', '错过来电', None, '18903752832', ''] itemshuxing = '电话' itemname = splititem[2] itemnumber = splititem[3] itemreceived = True itemcontent = None elif itemnamestr.endswith('的通话记录'): [_, itemname, *__] = itemnamestr.split() patterncall6 = re.compile( r',(?:(\+?\d+)s\s\(\d{2}:\d{2}:\d{2}\))?(\+?\d+)\s\((?:(已接来电)|(已拨电话)|(未接来电))\)' ) splititem = re.split(patterncall6, itemcontent) if len(splititem) == 7: itemshuxing = '电话' itemnumber = splititem[2] itemcontent = splititem[1] if splititem[4] is not None: itemreceived = False else: pass elif itemnamestr.startswith('SMS with'): itemshuxing = '短信' [*__, itemname] = itemnamestr.split() if itemcontent.startswith('收到'): itemreceived = True weiyi = 4 elif itemcontent.startswith('发出'): itemreceived = False weiyi = 4 else: weiyi = 1 itemcontent = itemcontentstr[weiyi:] # print(itemname + '\t' + str(itemtime) + '\t' + itemcontent) # print(splititem) item.append(itemtime) if itemname is None: itemname = itemnumber item.append(itemname) item.append(itemnumber) item.append(itemshuxing) item.append(itemreceived) item.append(itemcontent) # print(slices[i*5].strip()+'\t'+slices[i*5+1].strip()+'\t'+slices[i*5+2].strip()+'\t'+slices[i*5+4].strip()) split_items.append(item) dfnoti = pd.DataFrame(split_items, columns=('atime', 'name', 'number', 'shuxing', 'received', 'content')) log.info('电话短信记录整理后有%d条。' % dfnoti.shape[0]) dfnoti.drop_duplicates(inplace=True) log.info('电话短信记录去重后有%d条。' % dfnoti.shape[0]) dfnoti.sort_values(['atime'], ascending=False, inplace=True) # dfnoti.index = dfnoti['time'] # del dfnoti['time'] dfout = dfnoti # b3a3e458-f05b-424d-8ec1-604a3e916724 try: notestore = get_notestore() cfplife, inilifepath = getcfp('everlilfe') if cfplife.has_option('allsets', 'callsms'): ready2update = dfout.shape[0] > cfplife.getint( 'allsets', 'callsms') else: ready2update = True if ready2update: xiangmu = '电话短信' imglist2note(notestore, [], cfplife.get('notesguid', xiangmu), xiangmu, tablehtml2evernote(dfout[:1000], xiangmu)) cfplife.set('allsets', 'callsms', '%d' % dfout.shape[0]) cfplife.write(open(inilifepath, 'w', encoding='utf-8')) except Exception as eee: log.critical('更新人脉记录笔记时出现错误。%s' % str(eee)) return dfout
def caiwu2note(itemname, itemnameini, rstdf, clnameswithindex): print(f"<{itemname}>数据文本有效条目数:{rstdf.shape[0]}") shoukuanfixguid = getinivaluefromnote('webchat', f'{itemname}补') fixdf = getfix4finance(shoukuanfixguid, clnameswithindex) print(f"<{itemname}>修正笔记有效条目数:{fixdf.shape[0]}") alldf = rstdf.append(fixdf) bcount = alldf.shape[0] # 索引去重 alldf = alldf[~alldf.index.duplicated()] acount = alldf.shape[0] print(f"{bcount}\t{acount}") alldf.sort_index(ascending=False, inplace=True) # print(alldf) print(f"<{itemname}>综合有效条目数:{alldf.shape[0]}") finance2note(alldf.shape[0], alldf, f'{itemname}', f"{itemnameini}", f'{itemname}记录') alldf['time'] = alldf.index alldf['date'] = alldf['time'].apply( lambda x: pd.to_datetime(x).strftime('%F')) alldf['amount'] = alldf['amount'].astype(float) agg_map = {'daycount': 'count', 'amount': 'sum'} alldfgrpagg = alldf.groupby('date', as_index=False).agg(agg_map) alldfgrpagg.columns = ['date', 'lcount', 'lsum'] alldf.drop_duplicates(['date'], keep='first', inplace=True) finaldf = pd.merge(alldf, alldfgrpagg, how='outer', on=['date']) alldf = finaldf.loc[:, ['date', 'lcount', 'daycount', 'daysum', 'lsum']] # 日期,记录项目计数,记录项目最高值,记录项目累计值,记录项目求和 alldf.columns = ['date', 'lc', 'lh', 'lacc', 'lsum'] shoukuanrihuizongguid = getinivaluefromnote('webchat', f'{itemname}总手动') rhzdengjidf = getfix4finance(shoukuanrihuizongguid, ['date', 'count', 'sum']) duibidf = pd.merge(alldf, rhzdengjidf, how='outer', on=['date']) # 判断覆盖天数是否需要更新 itemslcsum = duibidf['lc'].sum() print(f"<{itemname}>记录条目数量:{itemslcsum}") itemslcsumini = getcfpoptionvalue('everwebchat', 'finance', f'{itemname}lcsum') print(f"<{itemname}>记录数量(ini)\t{itemslcsumini}") if not itemslcsumini: itemslcsumini = 0 if itemslcsumini == itemslcsum: # and False: print(f"<{itemname}>收款记录数量无变化") return duibidf.set_index('date', inplace=True) duibidf.sort_index(ascending=False, inplace=True) col_names = list(duibidf.columns) col_names.append('check') duibidf = duibidf.reindex(columns=col_names) # 通过0的填充保证各列数据可以运算 duibidf.fillna(0, inplace=True) duibidf['lacc'] = duibidf['lacc'].astype(float) duibidf['sum'] = duibidf['sum'].astype(float) duibidf['count'] = duibidf['count'].astype(int, errors='ignore') # print(duibidf.dtypes) duibidf['check'] = duibidf['lc'] - duibidf['count'] duibidf['check'] = duibidf['check'].apply(lambda x: '待核正' if (x != 0) else '') duibiguid = getinivaluefromnote('webchat', f'{itemname}核对') title = f'{itemname}核对' notecontent = tablehtml2evernote(duibidf, title) imglist2note(get_notestore(), [], duibiguid, title, notecontent) setcfpoptionvalue('everwebchat', 'finance', f'{itemname}lcsum', f"{itemslcsum}")
imglst.append(str(imgpathtoday)) dsdays = ds.resample('D').sum() print(dsdays) dsdays.plot() imgpathdays = dirmainpath / 'img' / 'gpsdays.png' touchfilepath2depth(imgpathdays) plt.savefig(str(imgpathdays)) plt.close() imglst.append(str(imgpathdays)) print(imglst) if (device_name := getinivaluefromnote('device', device_id)) is None: device_name = device_id imglist2note( get_notestore(), imglst, guid, f'手机_{device_name}_location更新记录', tablehtml2evernote( df4dis.sort_index(ascending=False).iloc[:100, ], "坐标流水记录单")) # %% [markdown] # ## 主函数main # %% if __name__ == '__main__': if not_IPython(): log.info(f'运行文件\t{__file__}……') dout = chuli_datasource() foot2show(dout) # showdis() if not_IPython(): log.info(f"完成文件{__file__}\t的运行")
def jinchustat(jinchujiluall, noteinfos): """ 读取ifttt自动通过gmail转发至evernote生成的地段进出记录,统计作图展示 :rtype: Null """ address = noteinfos[0] destguid = noteinfos[2] notetitle = noteinfos[3] # print(destguid, end='\t') # print(notetitle) dfjc = jinchujiluall[jinchujiluall.address == address][['entered']] # descdb(dfjc) dfjc['atime'] = dfjc.index dfjc['nianyue'] = dfjc['atime'].apply( lambda x: datetime.datetime.strftime(x, '%Y%m')) dfjc['小时'] = dfjc['atime'].apply( lambda x: datetime.datetime.strftime(x, '%H')) # print(dfjc.tail(10)) dfff = dfjc[dfjc.entered == False].groupby( ['nianyue', '小时'])['entered'].count() # 以离开为进出标准 dffu = dfff.unstack(fill_value=0) # print(dffu) # 补满小时数和年月份 for i in range(24): colname = '%02d' % i if colname not in list(dffu.columns): dffu[colname] = 0 lst = sorted(list(dffu.columns)) dffu = dffu[lst] dfrange = pd.date_range(dfjc.index.min(), dfjc.index.max() + MonthBegin(), freq='M') ddrange = pd.Series(dfrange).apply( lambda x: datetime.datetime.strftime(x, "%Y%m")) dffu = dffu.reindex(ddrange, fill_value=0) # 列尾行尾增加汇总 dffu['行合计'] = dffu.apply(lambda x: x.sum(), axis=1) dffu.loc['列合计'] = dffu.apply(lambda x: x.sum()) # print(dffu) lastestitems = tablehtml2evernote(dfjc[['entered']].head(5), notetitle.replace('统计图表', '记录')) stattable = tablehtml2evernote(dffu.sort_index(ascending=False), notetitle) hout = lastestitems + stattable # print(h) # print(hout) # 删掉列尾行尾的合计,并时间序列化index,为plot做准备 del dffu['行合计'] dffu = dffu.iloc[:-1] dffu['nianyue'] = dffu.index dffu['nianyue'] = dffu['nianyue'].apply( lambda x: pd.to_datetime("%s-%s-01" % (x[:4], x[-2:]))) dffu.index = dffu['nianyue'] del dffu['nianyue'] # descdb(dffu) plt.figure() ax1 = plt.subplot2grid((5, 2), (0, 0), colspan=2, rowspan=2) # print(dffu.sum()) ax1.plot(dffu.sum(axis=1).T) ax2 = plt.subplot2grid((5, 2), (3, 0), colspan=2, rowspan=2) # print(dffu.sum(axis=1).T) ax2.plot(dffu.sum()) imglist = [] # plt.show() # global dirmainpath img_jinchu_path = str(dirmainpath / 'img' / 'jichubyfgongsi.png') plt.savefig(img_jinchu_path) imglist.append(img_jinchu_path) # print(imglist) plt.close() imglist2note(get_notestore(), imglist, destguid, notetitle, hout)
if (dfwificountini := getcfpoptionvalue('everlife', 'wifi', 'itemnumber')): # 新数据集记录数和存档比较 ntupdatenum = dfwificount > dfwificountini else: ntupdatenum = True print(ntupdatenum, end='\t') print('WIFI连接记录', end='\t') print(dfwifi.index[0]) if ntupdatenum: # or True: dfnotename.reset_index(inplace=True) dfnotenameg = pd.DataFrame( dfnotename.groupby(['address']).max()['atime']) dfnotenameg['count'] = dfnotename.groupby(['address' ]).count()['atime'] dfnotenameg.sort_values(['atime'], ascending=False, inplace=True) wifitongjinametablestr = tablehtml2evernote( dfnotenameg, 'WIFI(特定)统计') wifijilutablestr = tablehtml2evernote(dfnotename.head(50), 'WIFI(特定)连接记录') dfwifiall.reset_index(inplace=True) dfnoteallg = pd.DataFrame( dfwifiall.groupby(['name']).max()['atime']) dfnoteallg['count'] = dfwifiall.groupby(['name']).count()['atime'] dfnoteallg.sort_values(['atime'], ascending=False, inplace=True) wifitongjialltablestr = tablehtml2evernote(dfnoteallg, 'WIFI(全部)统计') imglist2note( notestore, [], '971f14c0-dea9-4f13-9a16-ee6e236e25be', 'WIFI连接统计表', wifitongjinametablestr + wifijilutablestr + wifitongjialltablestr) setcfpoptionvalue('everlife', 'wifi', 'itemnumber',
def planfenxifunc(): # global dbpathworkplan cnxp = lite.connect(dbpathworkplan) tablename_updated = 'planupdated' errorshowstr = '更新业务日志汇总笔记时出现错误。' try: # fetchattendance_from_evernote() note_store = get_notestore() # 从印象笔记中取得在职业务列表 persons = BeautifulSoup( note_store.getNoteContent('992afcfb-3afb-437b-9eb1-7164d5207564'), 'html.parser') # print(persons) pslist = list() patn = re.compile('\s*[,,]\s*') for ddiv in persons.find_all('div'): psitem = re.split(patn, ddiv.get_text()) if len(psitem) == 3: atom = list() atom.append(psitem[0]) atom.append(pd.to_datetime(psitem[1])) atom.append(pd.to_datetime(psitem[2])) pslist.append(atom) personsdf = pd.DataFrame(pslist, columns=['name', 'shanggang', 'ligang']) personsdfzaigang = personsdf[pd.isnull(personsdf['ligang'])] # print(personsdfzaigang) persons = list(personsdfzaigang['name']) print(persons) evernoteapijiayi() # 更新相应数据表内容 updatedb_workplan(note_store, persons) # 从数据表中查询日志更新记录,只针对在职业务人员 # 组装sql语句使用的set,形如('梅富忠','陈益','周莉') personsetstr = '(' for pr in ['\'' + x + '\'' for x in persons]: personsetstr += pr + ',' personsetstr = personsetstr[:-1] + ')' # print(personsetstr) sqlstr = f'select distinct * from {tablename_updated} where name in {personsetstr} ' \ f'order by date desc, name, updatedtime desc' dfsource = pd.read_sql(sqlstr, cnxp, parse_dates=['date', 'updatedtime']) # 通过ini中记录的有效日志条目(以日期为单位)数量判断是否有有效的日志更新条目 updatablelist = [] for person in persons: planitemscount = dfsource.loc[dfsource.name == person].shape[0] print(f'{person}日志更新记录有{planitemscount}条') if cfpworkplan.has_option('业务计划总结itemscount', person): updatable = planitemscount > cfpworkplan.getint( '业务计划总结itemscount', person) else: updatable = True updatablelist.append(updatable) print(f'{persons},{updatablelist}') updatableall = False for i in range(len(updatablelist)): updatableall |= updatablelist[i] if updatableall: break if updatableall: # or True: dayscount = cfpworkplan.getint('业务计划总结dayscount', 'count') today = pd.to_datetime(datetime.datetime.today().strftime('%F')) workdays = isworkday([today - datetime.timedelta(days=60)], '全体', fromthen=True) # print(workdays) dtqishi = workdays[workdays.work].groupby( 'date').count().sort_index(ascending=False).index[dayscount - 1] print(f'最近{dayscount}个工作日(公司)的起始日期:{dtqishi}') # dtqishi = today - datetime.timedelta(days=dayscount) dtqujian = pd.date_range(dtqishi, today, freq='D').values dfqujian = pd.DataFrame() for person in persons: resultlist = isworkday(dtqujian, person) dftmp = pd.DataFrame( resultlist, columns=['date', 'name', 'work', 'xingzhi', 'tianshu']) shanggangdate = personsdf[personsdf.name == person]['shanggang'].values[0] dftmp = dftmp[dftmp.date >= shanggangdate] if dfqujian.shape[0] == 0: dfqujian = dftmp else: dfqujian = dfqujian.append(dftmp) dfsourcequjian = dfsource[dfsource.date >= dtqishi] # print(dfqujian) # print(dfsourcequjian) dfresult = pd.merge(dfqujian, dfsourcequjian, on=['date', 'name'], how='outer') # dflast = dfresult[dfresult.work == True].sort_values(['date', 'name'], ascending=[False, True]) dflast = dfresult.sort_values(['date', 'name'], ascending=[False, True]) df = dflast.loc[:, [ 'name', 'date', 'contentlength', 'updatedtime', 'xingzhi', 'tianshu' ]] df2show = df.drop_duplicates(['name', 'date'], keep='last') print(f'去重前记录数为:{df.shape[0]},去重后记录是:{df2show.shape[0]}') df2show.columns = ['业务人员', '计划日期', '内容字数', '日志更新时间', '出勤', '天数'] # print(df2show) def hege(a, b, c): freelist = ['请假'] if c in freelist: return f'{c}' if pd.isnull(b): # print(f'{a}\t{b}') return '未交' if pd.to_datetime(a) > pd.to_datetime(b): return '准时' else: return '延迟' col_names = list(df2show.columns) col_names.append('提交') df2show = df2show.reindex(columns=col_names) df2show.loc[:, ['提交']] = df2show.apply( lambda x: hege(x.计划日期, x.日志更新时间, x.出勤), axis=1) print(df2show[pd.isnull(df2show.日志更新时间)]) df2show['计划日期'] = df2show['计划日期'].apply( lambda x: x.strftime('%m-%d')) df2show['日志更新时间'] = df2show['日志更新时间'].apply( lambda x: x.strftime('%m-%d %H:%M') if pd.notnull(x) else '') df2show['内容字数'] = df2show['内容字数'].apply(lambda x: str(int(x)) if pd.notnull(x) else '') df2show = df2show.loc[:, [ '业务人员', '计划日期', '内容字数', '日志更新时间', '提交', '出勤' ]] freeday = ['周日'] df2show = df2show[df2show['出勤'].map(lambda x: x not in freeday)] # print(df2show) for ix in df2show.index: yanchiweitijiao = ['延迟', '未交'] queqin = ['请假'] if df2show.loc[ix]['出勤'] in queqin: for clname in df2show.columns: df2show.loc[ix, clname] = df2show.loc[ix, clname].join( ['<span style=\"color:gray\">', '</span>']) elif df2show.loc[ix]['提交'] in yanchiweitijiao: for clname in df2show.columns: df2show.loc[ix, clname] = df2show.loc[ix, clname] \ .join(['<span style=\"color:red\">', '</span>']) df2show = df2show.loc[:, ['业务人员', '计划日期', '内容字数', '日志更新时间', '提交']] # descdb(df2show) neirong = tablehtml2evernote(df2show, '业务工作日志提交情况汇总(最近%d工作日)' % dayscount, withindex=False) neirong = html.unescape(neirong) # print(neirong) guid = cfpworkplan.get('业务计划总结guid', '汇总') huizongnoteupdatedtime = datetime.datetime.now().strftime('%F %T') imglist2note(note_store, [], guid, '业务工作日志提交情况汇总(%s)' % huizongnoteupdatedtime, neirong) for person in persons: dfperson = dfsource.loc[dfsource.name == person] planitemscount = dfperson.shape[0] if cfpworkplan.has_option('业务计划总结itemscount', person): updatable = planitemscount > cfpworkplan.getint( '业务计划总结itemscount', person) else: updatable = True if updatable: if dfperson.shape[0] == 0: continue log.info('%s的业务日志条目数增加至%d,日志更新表中最新日期为:%s。' % (person, planitemscount, str(dfperson.iloc[0]['nianyueri']))) cfpworkplan.set('业务计划总结itemscount', person, '%d' % planitemscount) cfpworkplan.write(open(iniworkplanpath, 'w', encoding='utf-8')) except OSError as ose: if ose.errno == 10054: log.critical(f'远程主机发脾气了,强行断线。') log.critical(f'{errorshowstr}Windows错误:{ose}') except AttributeError as ae: log.critical(f'{errorshowstr}属性错误:{ae}') except Exception as eee: log.critical(f'{errorshowstr}{eee}') # raise eee finally: cnxp.close()
def showorderstat(): # xlsfile = 'data\\work\\销售订单\\销售订单20180606__20180607034848_480667.xls' # dforder = chulixls_order(xlsfile) # global workplannotebookguid workplannotebookguid = '2c8e97b5-421f-461c-8e35-0f0b1a33e91c' pathor = dirmainpath / 'data' / 'work' / '销售订单' dforder = chulidataindir_order(pathor) # print(dforder.dtypes) dingdanxiaoshouyuedufenxi(dforder) dforder = dforder.loc[:, ['日期', '订单编号', '区域', '类型', '客户名称', '业务人员', '订单金额']] dforder.sort_values(by=['日期', '订单编号', '业务人员'], ascending=False, inplace=True) zuixinriqi = dforder.groupby(['日期'])['日期'].size().index.max() orderdatestr = zuixinriqi.strftime('%F') print(orderdatestr, end='\t') dforderzuixinriqi = dforder[dforder.日期 == zuixinriqi] print(dforderzuixinriqi.shape[0]) persons = list(dforderzuixinriqi.groupby('业务人员')['业务人员'].count().index) # print(persons) notestr = '每日销售订单核对' if cfpzysm.has_section(notestr) is False: cfpzysm.add_section(notestr) cfpzysm.write(open(inizysmpath, 'w', encoding='utf-8')) for person in persons: if cfpzysm.has_option(notestr + 'guid', person) is False: try: notestore = get_notestore() plannote = ttypes.Note() plannote.title = notestr + person nbody = '<?xml version="1.0" encoding="UTF-8"?>' nbody += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' nbody += '<en-note>%s</en-note>' % plannote.title plannote.content = nbody plannote.notebookGuid = workplannotebookguid token = cfp.get('evernote', 'token') note = notestore.createNote(token, plannote) evernoteapijiayi() cfpzysm.set(notestr + 'guid', person, '%s' % note.guid) cfpzysm.write(open(inizysmpath, 'w', encoding='utf-8')) log.info('成功创建%s的%s笔记' % (person, notestr)) except Exception as ee: log.critical('创建%s的%s笔记时出现错误。%s' % (person, notestr, str(ee))) continue if cfpzysm.has_option(notestr + 'guid', person + '最新订单日期'): ordertoday = cfpzysm.get(notestr + 'guid', person + '最新订单日期') if zuixinriqi <= pd.to_datetime(ordertoday): # and False: # 调试开关,强行生成图表 continue dfperson = dforderzuixinriqi[dforderzuixinriqi.业务人员 == person] dfpersonsum = dfperson.groupby('业务人员').sum()['订单金额'] del dfperson['业务人员'] del dfperson['日期'] print(person, end='\t') print(dfpersonsum[0], end='\t') personguid = cfpzysm.get(notestr + 'guid', person) print(personguid) neirong = tablehtml2evernote(dftotal2top(dfperson), f'{orderdatestr}{notestr}——{person}', withindex=False) # print(neirong) try: notestore = get_notestore() imglist2note(notestore, [], personguid, '%s——%s(%s)' % (notestr, person, orderdatestr), neirong) cfpzysm.set(notestr + 'guid', person + '最新订单日期', '%s' % orderdatestr) cfpzysm.write(open(inizysmpath, 'w', encoding='utf-8')) except Exception as eeee: log.critical('更新笔记%s——%s(%s)时出现严重错误。%s' % (notestr, person, orderdatestr, str(eeee))) else: log.info('下列人员的销售订单正常处置完毕:%s' % persons) yuechuriqi = pd.to_datetime(f"{zuixinriqi.strftime('%Y')}-{zuixinriqi.strftime('%m')}-01") dfsales = pd.DataFrame(dforder[dforder.日期 >= yuechuriqi]) dfsales = dfsales.groupby(['区域', '类型', '客户名称', '业务人员'], as_index=False).sum() dfsales.sort_values(['区域', '订单金额'], inplace=True) notestr = '销售订单金额(月)' if cfpzysm.has_section(notestr) is False: cfpzysm.add_section(notestr) cfpzysm.write(open(inizysmpath, 'w', encoding='utf-8')) for person in persons: if cfpzysm.has_option(notestr, person) is False: try: notestore = get_notestore() plannote = ttypes.Note() plannote.title = notestr + person nbody = '<?xml version="1.0" encoding="UTF-8"?>' nbody += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' nbody += '<en-note>%s</en-note>' % plannote.title plannote.content = nbody plannote.notebookGuid = workplannotebookguid # cfp, cfppath = getcfp('everwork') token = cfp.get('evernote', 'token') note = notestore.createNote(token, plannote) evernoteapijiayi() cfpzysm.set(notestr, person, '%s' % note.guid) cfpzysm.write(open(inizysmpath, 'w', encoding='utf-8')) log.info('成功创建%s的%s笔记' % (person, notestr)) except Exception as ee: log.critical('创建%s的%s笔记时出现错误。%s' % (person, notestr, str(ee))) continue if cfpzysm.has_option(notestr, person + '最新订单日期'): ordertoday = cfpzysm.get(notestr, person + '最新订单日期') if zuixinriqi <= pd.to_datetime(ordertoday): # and False: continue dfperson = dfsales[dfsales.业务人员 == person] dfpersonsum = dfperson['订单金额'].sum() del dfperson['业务人员'] print(person, end='\t') print(dfpersonsum, end='\t') personguid = cfpzysm.get(notestr, person) print(personguid) neirong = tablehtml2evernote(dftotal2top(dfperson), f'{orderdatestr[:-3]}{notestr}', withindex=False) # print(neirong) try: notestore = get_notestore() imglist2note(notestore, [], personguid, '%s——%s(%s)' % (notestr, person, orderdatestr[:-3]), neirong) cfpzysm.set(notestr, person + '最新订单日期', '%s' % orderdatestr) cfpzysm.write(open(inizysmpath, 'w', encoding='utf-8')) except Exception as eeee: log.critical('更新笔记%s——%s(%s)时出现严重错误。%s' % (notestr, person, orderdatestr, str(eeee))) else: log.info('下列人员的销售订单金额月度分析正常处置完毕:%s' % persons) dfsales = pd.DataFrame(dforder) dfsales = dfsales.groupby(['日期', '业务人员'], as_index=False).sum() # persons = list(dfsales.groupby('业务人员')['业务人员'].count().index) # print(persons) dfsales.sort_values(['日期'], inplace=True) notestr = '销售金额分析图表' if cfpzysm.has_section(notestr) is False: cfpzysm.add_section(notestr) cfpzysm.write(open(inizysmpath, 'w', encoding='utf-8')) for person in persons: if cfpzysm.has_option(notestr, person) is False: try: notestore = get_notestore() plannote = ttypes.Note() plannote.title = notestr + person nbody = '<?xml version="1.0" encoding="UTF-8"?>' nbody += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' nbody += '<en-note>%s</en-note>' % plannote.title plannote.content = nbody plannote.notebookGuid = workplannotebookguid # cfp, cfppath = getcfp('everwork') token = cfp.get('evernote', 'token') note = notestore.createNote(token, plannote) evernoteapijiayi() cfpzysm.set(notestr, person, '%s' % note.guid) cfpzysm.write(open(inizysmpath, 'w', encoding='utf-8')) log.info('成功创建%s的%s笔记' % (person, notestr)) except Exception as ee: log.critical('创建%s的%s笔记时出现错误。%s' % (person, notestr, str(ee))) continue dfperson = dfsales[dfsales.业务人员 == person] zuixinriqi = dfperson.groupby(['日期'])['日期'].size().index.max() orderdatestr = zuixinriqi.strftime('%F') if cfpzysm.has_option(notestr, person + '最新订单日期'): ordertoday = cfpzysm.get(notestr, person + '最新订单日期') # print(f'{zuixinriqi}\t{ordertoday}') if zuixinriqi <= pd.to_datetime(ordertoday): # and False: # 调试开关,强行生成图表 continue dfpersonsum = dfperson['订单金额'].sum() dfperson = dfperson.groupby(['日期']).sum() # del dfperson['业务人员'] print(person, end='\t') print(dfpersonsum, end='\t') personguid = cfpzysm.get(notestr, person) print(personguid) # neirong = tablehtml2evernote(dftotal2top(dfperson), f'{orderdatestr[:-3]}{notestr}', withindex=False) neirong = "" # print(neirong) try: notestore = get_notestore() imglist = dfin2imglist(dfperson, cum=True) imglist2note(notestore, imglist, personguid, '%s——%s(%s)' % (notestr, person, orderdatestr[:-3]), neirong) cfpzysm.set(notestr, person + '最新订单日期', '%s' % orderdatestr) cfpzysm.write(open(inizysmpath, 'w', encoding='utf-8')) except Exception as eeee: log.critical('更新笔记%s——%s(%s)时出现严重错误。%s' % (notestr, person, orderdatestr, str(eeee))) else: log.info('下列人员的销售金额分析图表正常处置完毕:%s' % persons)
def dingdanxiaoshouyuedufenxi(dforder): dfall = dforder.loc[:, :] dfall['年月'] = dfall['日期'].apply(lambda x: x.strftime('%Y%m')) # descdb(dfall) zuijinchengjiaori = max(dfall['日期']) print(f'数据集最新日期:{zuijinchengjiaori.strftime("%F")}') if cfpdata.has_option('ordersaleguidquyu', '数据最新日期'): daterec = pd.to_datetime(cfpdata.get('ordersaleguidquyu', '数据最新日期')) if daterec >= zuijinchengjiaori: # and False: log.info(f'订单数据集无更新,返回') return zuiyuanriqi = zuijinchengjiaori + datetime.timedelta(days=-365) zuiyuanyuechu = pd.to_datetime(f"{zuiyuanriqi.strftime('%Y-%m')}-01") print(zuiyuanyuechu.strftime("%F")) dfkehu = dfall.groupby(['单位编号', '客户名称', '区域', '类型'], as_index=False).count() dfkehu.drop_duplicates(['单位编号'], keep='last', inplace=True) dfkehuzhengli = dfkehu[['单位编号', '客户名称', '区域', '类型']] dfkehuzhengli.index = dfkehuzhengli['单位编号'] del dfkehuzhengli['单位编号'] # descdb(dfkehuzhengli) dsquyuzuixinriqi = pd.Series(dfall[dfall.日期 == zuijinchengjiaori].groupby('区域').count().index.values) # print(dsquyuzuixinriqi.values) dfyuetongji = dfall[dfall.日期 >= zuiyuanyuechu].groupby(by=['年月', '单位编号'], as_index=False, sort=True)['订单金额'].sum() dfyuetongji['订单金额'] = dfyuetongji['订单金额'].astype(int) # descdb(dfyuetongji) dfpivot = dfyuetongji.pivot(index='单位编号', values='订单金额', columns='年月') dfpivot = pd.DataFrame(dfpivot) cls = list(dfpivot.columns) # print(cls) # for cl in cls: # dfpivot[cl] = dfpivot[cl].astype(int) dfpivot['单位编号'] = dfpivot.index dfpivot['客户名称'] = dfpivot['单位编号'].apply(lambda x: dfkehuzhengli.loc[x][0]) dfpivot['区域'] = dfpivot['单位编号'].apply(lambda x: dfkehuzhengli.loc[x][1]) dfpivot['类型'] = dfpivot['单位编号'].apply(lambda x: dfkehuzhengli.loc[x][2]) clsnew = ['客户名称', '区域', '类型'] + cls # print(clsnew) dfpivot = dfpivot.loc[:, clsnew] dfpivot['成交月数'] = dfpivot.apply(lambda x: x[-13:].count(), axis=1) def shouciyuefen(xx): for i in range(len(xx)): # print(xx[i]) if xx[i] > 0: return i else: return 12 dfpivot['首次成交月份'] = dfpivot.apply(lambda x: clsnew[shouciyuefen(x[3:16]) + 3], axis=1) dfpivot['首交月数'] = dfpivot.apply(lambda x: 13 - shouciyuefen(x[3:16]), axis=1) def zuijinyuefen(xx): for i in range(len(xx) - 1, -1, -1): # print(f'{xx[i]}\t{i}') if xx[i] > 0: return i else: return 12 dfpivot['最近成交月份'] = dfpivot.apply(lambda x: clsnew[zuijinyuefen(x[3:16]) + 3], axis=1) dfpivot['尾交月数'] = dfpivot.apply(lambda x: 13 - zuijinyuefen(x[3:16]), axis=1) dfpivot['有效月数'] = dfpivot['首交月数'] - dfpivot['尾交月数'] + 1 dfpivot.fillna(0, inplace=True) dfpivot['年总金额'] = dfpivot.apply(lambda x: sum(x[3:16]), axis=1) # print(dfpivot.iloc[0, :]) dfpivot['年总金额'] = dfpivot['年总金额'].astype(int) def youxiaoyuejun(jine, yueshu): if yueshu == 0: return 0 else: return jine / yueshu dfpivot['有效月均'] = dfpivot.apply(lambda x: youxiaoyuejun(x.年总金额, x.有效月数), axis=1) dfpivot['有效月均'] = dfpivot['有效月均'].astype(int) dfpivot.sort_values(['区域', '有效月均'], ascending=[True, False], inplace=True) # descdb(dfpivot) # clsnewnew = clsnew + ['有效月均', '年总金额'] # dfout = dfpivot[(dfpivot.有效月均 > 300) | (dfpivot.首交月数 < 5)] # print(dfout[dfout.首交月数 < 5]) dfout = dfpivot # dfshow = dfout.loc[:, clsnewnew] dfshow = dfout.loc[:, :] dfshow.fillna(0, inplace=True) for cl in cls: dfshow[cl] = dfshow[cl].astype(int) # print(dfshow[dfshow.类型 == 'I']) # descdb(dfshow) cnx = lite.connect(dbpathworkplan) # dfshow.to_sql('tmptable', cnx, index=True, if_exists='replace') cursor = cnx.cursor() cursor.execute(f'attach database \'{dbpathquandan}\' as \'C\'') dfquyu = pd.read_sql('select * from C.quyu', cnx, index_col='index') dfquyu.drop_duplicates(['区域'], keep='first', inplace=True) dfquyu.index = dfquyu['区域'] del dfquyu['区域'] # descdb(dfquyu) dfleixing = pd.read_sql('select * from C.leixing', cnx, index_col='index') dfleixing.index = dfleixing['编码'] del dfleixing['编码'] # descdb(dfleixing) dfshow['区域名称'] = dfshow['区域'].apply(lambda x: dfquyu.loc[x][0]) # print(dfshow[dfshow.类型 == '0']) dfshow['类型小类'] = dfshow['类型'].apply(lambda x: dfleixing.loc[x][0]) dfshow['类型大类'] = dfshow['类型'].apply(lambda x: dfleixing.loc[x][1]) # descdb(dfshow) cnx.close() writer = pd.ExcelWriter(str(dirmainpath / 'data' / '客户销售总表.xlsx')) dfzhongduan = dfshow[dfshow.类型大类 == '终端客户'] # dfzhongduan.to_excel('test4kehuquandan.xlsx') # print(dfzhongduan.dtypes) # print(dfzhongduan.head(5)) pd.DataFrame(dfzhongduan).to_excel(writer, sheet_name='客户销售全单') log.info('成功输出《客户销售全单》') writer.close() # descdb(dfzhongduan) targetlist = list() dfzdall = dfzhongduan.loc[:, :] # descdb(dfzdall) notestore = get_notestore() quyuset = set(dsquyuzuixinriqi.apply(lambda x: dfquyu.loc[x][0]).values) print(quyuset) # quyuset = set(list(dfzdall['区域名称'])) for qy in quyuset: dfslice = dfzdall[dfzdall.区域名称 == qy] dfslicesingle = dfslice.loc[:, :] del dfslicesingle['区域名称'] # descdb(dfslicesingle) print(qy, end='\t') print(dfslicesingle.shape[0], end='\t') # descdb(dfslicesingle) if cfpdata.has_option('guidquyunb', qy): nbguid = cfpdata.get('guidquyunb', qy) else: try: notebook = ttypes.Notebook() notebook.name = qy notebook = notestore.createNotebook(notebook) nbguid = notebook.guid cfpdata.set('guidquyunb', qy, nbguid) cfpdata.write(open(inidatanotefilepath, 'w', encoding='utf-8')) except OSError as eeeee: nbguid = None log.critical(f'创建《{qy}》笔记本时出现错误。{eeeee}') print(nbguid, end='\t') if cfpdata.has_option('ordersaleguidquyu', qy + 'guid'): ntguid = cfpdata.get('ordersaleguidquyu', qy + 'guid') else: try: note = ttypes.Note() note.title = qy + "订单金额年度分析" note.content = '<?xml version="1.0" encoding="UTF-8"?>' \ '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' note.content += '<en-note>专营休闲美食</en-note>' note.notebookGuid = nbguid note = notestore.createNote(note) evernoteapijiayi() ntguid = note.guid cfpdata.set('ordersaleguidquyu', qy + 'guid', ntguid) cfpdata.write(open(inidatanotefilepath, 'w', encoding='utf-8')) except OSError as ee: ntguid = None log.critical(f'创建《{qy}订单金额年度分析》笔记时出现错误。{ee}') print(ntguid) target = list() target.append(qy) target.append(ntguid) target.append(dfslicesingle) targetlist.append(target) # descdb(dfshow[dfshow.index.str.find('XF') >= 0]) lqzlist = [['连锁客户', '0e8ba322-4874-4627-a6de-13c69fffc88d'], ['渠道客户', '4AC03027-5929-415B-9711-0A8170263189'.lower()], ['直销客户', 'B0731D74-C268-417E-A8B7-7BE3EE590FAE'.lower()] ] for [mingmu, mmguid] in lqzlist: dfls = dfshow.loc[:, :] dfls = dfls[dfls.类型大类 == mingmu] # dfls = dfls[str(dfls.index)[7:9] == 'XF'] del dfls['类型大类'] dfls.sort_values(['有效月均'], ascending=False, inplace=True) target = list() target.append(mingmu) target.append(mmguid) target.append(dfls) targetlist.append(target) lslist = [['学府超市', 'XF', 'ce26a763-81cc-421f-8430-22dab21ba43e'], ['红生超市', 'HS', '1F0995DA-1DEC-4333-8EA6-8C85B54E2B71'.lower()], ['五分钟', 'WF', '41518B63-FF81-4719-BFE0-0E5BBFBC295A'.lower()] ] for [mingcheng, bianma, guid] in lslist: dfls = dfshow.loc[:, :] dfls = dfls[dfls.index.str.find(bianma) >= 0] del dfls['类型大类'] if dfls.shape[0] == 0: log.info(f'连锁超市{mingcheng}没有数据记录,跳过') continue dfls.sort_values(['有效月均'], ascending=False, inplace=True) target = list() target.append(mingcheng) target.append(guid) target.append(dfls) targetlist.append(target) # print(targetlist) for [qy, ntguid, dfslicesingle] in targetlist: try: dfzdclnames = list(dfslicesingle.columns) # print(dfzdclnames) dfzdclnames3 = dfzdclnames[3:16] dfzdclnamesnew = [dfzdclnames[0]] + [dfzdclnames[-2]] + dfzdclnames3 + [dfzdclnames[23]] + [dfzdclnames[22]] # print(dfzdclnamesnew) stattitle = f'总客户:{dfslicesingle.shape[0]},' \ f'在线客户(前三个月有成交记录):{dfslicesingle[dfslicesingle.尾交月数 <= 4].shape[0]},' \ f'本月成交客户:{dfslicesingle[dfslicesingle.尾交月数 == 1].shape[0]}' imglist2note(notestore, [], ntguid, qy + '订单金额年度分析', tablehtml2evernote(dftotal2top(dfslicesingle.loc[:, dfzdclnamesnew]), stattitle, withindex=False, setwidth=False)) cfpdata.set('ordersaleguidquyu', qy + 'count', f'{dfslicesingle.shape[0]}') cfpdata.write(open(inidatanotefilepath, 'w', encoding='utf-8')) log.info(f'{qy}数据项目成功更新') except OSError as eee: log.critical(f'《{qy}订单金额年度分析》笔记更新时出现错误。{eee}') # print(dfslicesingle.shape[0]) else: cfpdata.set('ordersaleguidquyu', '数据最新日期', f'{zuijinchengjiaori}') cfpdata.write(open(inidatanotefilepath, 'w', encoding='utf-8'))