01.制作模板
0101.文件名
daily_demo.txt
0102.txt里面的内容
mdOA运维简报:
一、今日工作完成情况:
1、本日受理工单daily_ticket_number单,完成daily_ticket_number单,遗留0单;
2、系统巡检1次,服务器及系统正常;
3、
02.运行 python的代码 D:\Study\myproject\Python_auto_office\auto_report\create_daily.py
#读取数据源
# #-*-coding:utf-8-*-
#导入时间功能的包
import time
#导入创建目录的包
# import util.create_dir as mkd
#导入复制模板的包
import util.copy_demo as cpd
#导入操作数据的包
# import pandas as pd
#获取日期出现的次数
import util.days_appear_times as dat
#导入操作txt的包(替换,追加)
import util.txt_handle as th
#导入获取含有关键字的行,格式是dataframe
import util.get_daily_record as gdr
#导入生成两位数字的包
import util.create_number_order as cno
#常量变量定义
default_path="D:\\work_report\\daily"
origen_path ="D:\\work_report\\daily\\daily_demo.txt"
current_time=time.strftime('%Y-%m-%d',time.localtime(time.time()))
current_year = time.strftime('%Y',time.localtime(time.time()))
current_month = time.strftime('%m',time.localtime(time.time()))
current_day=time.strftime('%d',time.localtime(time.time()))
current_month_day=time.strftime("%m月%d日",time.localtime(time.time()))
# week_of_month 获取当前时间是当月第几周
report_date = current_year+"年"+current_month+"月"+current_day+"日"
rename_path=default_path+"\\"+current_time+".txt"
data_path='D:\\work_report\\work_report_data.xlsx'
#############################20230514
#01.创建目录
# try:
# mkd.mkdir(default_path+"\\")
# except:
# print("目录已经存在")
#02.复制模板
cpd.cpdemo(origen_path,rename_path)
# #03.替换日期
# print(type(current_month_day))
th.replace_txt(rename_path,'md',str(current_month_day))
# 04.出现的次数
daily_ticket_number=int(dat.apper_times(current_time,data_path))
# 05.替换""daily_icket_number" 今天完成工单的次数
th.replace_txt(rename_path,'daily_ticket_number',str(daily_ticket_number))
# 05.获取关键字的记录,df格式
df=gdr.get_day_record(data_path,current_time)
order_list=cno.create_hundred_order_list(daily_ticket_number)
count=1
for index,row in df.iterrows():
record=row['date']+" "+row['departments']+" "+row['name']+" "+row['phone']+row['questions']+"\n"
count_new=str(count)+"." #这个是日报工单的序号
th.append_txt(rename_path,count_new)
th.append_txt(rename_path,record)
count=count+1
# file='D:\work_report\work_report_data.xlsx'
# data=pd.read_excel(file,header=[1])
# # data=pd.read_excel(file,usecols=[0,1,2,3,4])
# # data=pd.read_excel(file,usecols=['departments','name'])
# data=pd.read_excel(file,usecols=['date'])
# print(data)
复制模板的包
D:\Study\myproject\Python_auto_office\auto_report\util\copy_demo.py
# coding=utf-8
import shutil
def cpdemo(origen_path,final_path):
# print("cpdemo参数1: "+origen_path)
# print("cpdemo参数2: "+final_path)
shutil.copyfile(origen_path,final_path)
出现次数的包
D:\Study\myproject\Python_auto_office\auto_report\util\days_appear_times.py
#输入关键字,输出这个关键字出现的次数
from openpyxl import load_workbook
def apper_times(date,data_path):
wb = load_workbook(data_path) #读取excel文件
sheets = wb.worksheets[0] #读取目标sheet
m_row = sheets.max_row #获取sheet最大行数
equip = []
staname = []
count = []
col = 1 #筛选列
tar_col = 1 #目标列
for i in range(1,m_row+1):
if sheets.cell(i,col).value == date: #获取符合条件的关键列,类似筛选
equip.append(sheets.cell(i,tar_col ).value) #将符合筛选条件的项写入数组
print("equip第一个for:")
print(equip)
for x in equip:
if x not in staname:
staname.append(x) #获取没有重复项的数组
# print("stanme第二个for:")
# print(staname)
for target in staname:
# print ("数量"+ " " + str(equip.count(target))) #统计目标数组中每个重复项出现的次数
# print("第3个for:")
# print(str(equip.count(target)))
return str(equip.count(target))
处理txt的包
def replace_txt(filename,old_text,new_text):
with open(filename, "r" ) as file:
content = file.read()
print(content)
content = content.replace(old_text, new_text)
with open(filename, "w") as file:
file.write(content)
# if __name__ == '__main__':
# file_name="D:\\work_report\\daily\\2023-05-16.txt"
# a="month_and_day"
# b="5-16"
def append_txt(filename,text):
# with open(filename, "a" ) as file:
# content = file.read()
with open(filename, "a") as file:
file.write(text)
导入获取含有关键字的行,格式是dataframe的包
# 导入pandas库,并起别名为pd
import pandas as pd
def get_day_record(target_path,key_word):
# 这里的路径使用了绝对路径,根据目标文件的位置而定
target_path = 'D:\\work_report\\work_report_data.xlsx'
# 读取指定Excel表格
df = pd.read_excel(target_path)
# 使用.str.find()方法,如果cell中不包含"Python",则返回-1,否则返回查找到的"Python"的下标,那么“!= -1”则表示包含“Python”,把筛选出的新的DataFrame赋值给new_df.
new_df = df[(df['date'].str.find(key_word) != -1)]
return new_df