def get_not_edited(car_datetimes): '''获取昨天没编辑的车辆数 ''' not_edited = 0 current_date = public.date_current() for datetime_ in car_datetimes: car_date = public.datetime2date(datetime_) if (current_date - car_date).days == 1: not_edited += 1 return not_edited
def image_file(filename): '验证是否可提取拍照时间.' date_now = public.date_current() image_piar = operation.extract_from_image(filename) if not image_piar[1]: return '不能提取照片拍照时间,请设置相机以记录拍照时间' date_image = public.datetime2date(image_piar[1]) # print(date_now, date_image, (date_now - date_image).days) if (date_now - date_image).days > 1: return '{}:拍摄时间至少在2天前,不能处理'.format(image_piar[0]) return True
def get_sheets(book): '为跨月操作,需获取2个表单' sheets = [] date_current = public.date_current() month = date_current.month # 前一个月的表单 if 1 == date_current.day and 1 != month: sheets.append(get_sheet(book, month - 1)) else: sheets.append(None) # 当月表单 sheets.append(get_sheet(book, month)) return sheets
def add_entries(sheet, car_datetimes, row_start, default_lane=7, edited=0): ''' 添加完成后,删除后面所有行 @car_datetimes:已经排序好的datetime对象 pics_index:格式为“20170701001.1-001.3” ''' date_current = public.date_current() index_yesterday = edited index_today = 0 index_fake = 999 index = 0 for datetime_ in car_datetimes: date_car = public.datetime2date(datetime_) days = (date_current - date_car).days if 1 == days: index_yesterday += 1 index = index_yesterday elif 0 == days: index_today += 1 index = index_today else: index_fake += 1 index = index_fake pics_index = '{}{:02}{:02}{:03}.1-{:03}.3'.format( datetime_.year, datetime_.month, datetime_.day, index, index) # print('第{}行'.format(row_start), end='\t') add_entry(sheet, datetime_, row_start, default_lane=default_lane, pics_index=pics_index) row_start += 1 # 在重复修改时,如果有删减车辆数,可去除后面多余行 clear_rows_from(sheet, row_start)
def generate_path_pairs(file2datetimes, dst_folder, edited=0, inplace=True): ''' 返回值:元组(old_paths, new_paths, duplicated_paths) @files2datetimes: [(路径名, 创建日期), ....]序列 @dst_dir:目标文件夹 @edited:昨天编辑好的车辆数 ''' pics_per_car = 3 old_paths = [] new_paths = [] duplicated_paths = [] date_current = public.date_current() car_index_today = 0 car_index_yesterday = edited fake_index = 999 # 如果是“将来”的图片 car_index = 0 pic_index = 1 filepaths_in_dst_folder = public.filepaths_in_folder(dst_folder) for i, pair in enumerate(file2datetimes): path = pair[0] extension = os.path.splitext(path)[1] folder = os.path.dirname(path) datetime_car = pair[1] year = datetime_car.year month = datetime_car.month day = datetime_car.day date_car = datetime.datetime(year=year, month=month, day=day) # 计算车辆索引 if i % pics_per_car == 0: days = (date_current - date_car).days if 1 == days: car_index_yesterday += 1 car_index = car_index_yesterday elif 0 == days: car_index_today += 1 car_index = car_index_today else: fake_index += 1 car_index = fake_index # 计算照片索引 pic_index = (i % pics_per_car) + 1 # 构建路径对 # print(year, month, day, car_index, pic_index) pic_name = '{}{:02}{:02}{:03}-{}'.format(year, month, day, car_index, pic_index) if inplace: new_path = os.path.join(folder, pic_name + extension) else: new_path = os.path.join(dst_folder, pic_name + extension) # 向old_path、new_paths和duplicated_paths中添加数据 old_paths.append(path) new_paths.append(new_path) if new_path in filepaths_in_dst_folder: # print('重复') duplicated_paths.append(new_path) return (old_paths, new_paths, duplicated_paths)
def get_edited_and_row_start(excel_file, guard_datetime): ''' 获取已编辑车辆数,和开始插入的行 @excel_file:主要是sheet主要为了为operation.py提供接口 @not_edited:昨天没处理的车辆数 返回值:元组(昨天处理过的车辆数, 开始写入的行) ''' book = xls.load_workbook(excel_file) # 确定是否操作前一个的表单 sheets = get_sheets(book) sheet = sheets[0] or sheets[1] # 昨天出处理过的车辆数 edited = 0 # 开始写入的行 row_start = 0 # 有昨天条目的第一行 row_of_yesterday_start = 0 # 只需判断天数,所以重建 date_current = public.date_current() # # 循环获已编辑昨天的行数,以及昨天条目的第一行 # for r, row in enumerate(sheet.rows): # 跳过头信息部分 if r < len(HEADER): continue # 防止有空表格还记录在rows中, # 遍历后面的空行,造成插入在空行后 if not is_normal_row(row): # 此时r值为该插入行的上一行数 # 如果没遇到非正常行,遍历后r值也为 # 正常插入行的上一行 # openpyxl中起始索引为1 break row_datetime = get_datetime(row) if not row_datetime: continue row_date = public.datetime2date(row_datetime) date_delta = date_current - row_date if 1 == date_delta.days: if 0 == row_of_yesterday_start: row_of_yesterday_start = r + 1 if row_datetime < guard_datetime: edited += 1 # print(r, row_datetime) # 确定开始写入的行 # 如果没有昨天的行,有2种情况: # 1. 新建的的sheet # 2. 确实没有昨天的条目 # print('row_of_yesterday_start:', row_of_yesterday_start, '最后行号:', r + 1) if 0 == row_of_yesterday_start: if len(HEADER) == sheet.max_row: row_start = len(HEADER) + 1 else: row_start = r + 1 else: row_start = row_of_yesterday_start + edited return (edited, row_start)