7fc3f80e by 周伟奇

idcard monthly script

1 parent 15d73747
......@@ -14,7 +14,7 @@ class Command(BaseCommand, LoggerMixin):
def __init__(self):
super().__init__()
self.sheet_names = (consts.AFC_PREFIX, consts.HIL_PREFIX)
self.sheet_name = '身份证'
self.header = ('申请号', '身份证号', '民族', '时间戳')
def add_arguments(self, parser):
......@@ -35,21 +35,26 @@ class Command(BaseCommand, LoggerMixin):
else:
date_str = date.strftime('%Y-%m-%d')
excel_dir = os.path.join(conf.DATA_DIR, 'AFC', 'Logs')
if not os.path.exists(excel_dir):
print('excel dir not exists')
afc_excel_dir = os.path.join(conf.DATA_DIR, 'AFC', 'IdCard')
hil_excel_dir = os.path.join(conf.DATA_DIR, 'HIL', 'IdCard')
if not os.path.exists(afc_excel_dir) or not os.path.exists(hil_excel_dir):
print('excel_dir not exist')
return
excel_path = os.path.join(excel_dir, 'idcard_{0}.xlsx'.format(date_str))
log_path = os.path.join(conf.LOG_DIR, 'idcard.log.{0}'.format(date_str))
if not os.path.exists(log_path):
print('log_path not exists')
return
wb = Workbook()
for name in self.sheet_names:
ws = wb.create_sheet(name)
ws.append(self.header)
wb.remove(wb.get_sheet_by_name('Sheet'))
wb_afc = Workbook()
ws_afc = wb_afc.create_sheet(self.sheet_name)
ws_afc.append(self.header)
wb_afc.remove(wb_afc.get_sheet_by_name('Sheet'))
wb_hil = Workbook()
ws_hil = wb_hil.create_sheet(self.sheet_name)
ws_hil.append(self.header)
wb_hil.remove(wb_hil.get_sheet_by_name('Sheet'))
with open(log_path, 'r', encoding='utf-8') as fp:
for line in fp:
......@@ -73,8 +78,14 @@ class Command(BaseCommand, LoggerMixin):
doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc
application_id = doc_class.objects.filter(id=int(doc_id_str)).values_list('application_id', flat=True)
ws = wb.get_sheet_by_name(business_type)
for id_num, nation in content_list:
ws.append((application_id[0], id_num, nation, time_str))
if business_type == consts.HIL_PREFIX:
for id_num, nation in content_list:
ws_hil.append((application_id[0], id_num, nation, time_str))
else:
for id_num, nation in content_list:
ws_afc.append((application_id[0], id_num, nation, time_str))
wb.save(excel_path)
afc_excel_path = os.path.join(afc_excel_dir, 'idcard_{0}.xlsx'.format(date_str))
hil_excel_path = os.path.join(hil_excel_dir, 'idcard_{0}.xlsx'.format(date_str))
wb_afc.save(afc_excel_path)
wb_hil.save(hil_excel_path)
......
import os
import datetime
from calendar import monthrange
from openpyxl import Workbook, load_workbook
from django.core.management import BaseCommand
from settings import conf
from common.mixins import LoggerMixin
class Command(BaseCommand, LoggerMixin):
def __init__(self):
super().__init__()
self.dirs = ('AFC', 'HIL')
def handle(self, *args, **kwargs):
now_time = datetime.datetime.now()
end_day_in_mouth = now_time.replace(day=1)
pre_mouth = end_day_in_mouth - datetime.timedelta(days=1)
for target_dir in self.dirs:
excel_dir = os.path.join(conf.DATA_DIR, target_dir, 'IdCard')
if not os.path.exists(excel_dir):
print('excel dir not exists: {0}'.format(excel_dir))
return
monthly_wb = Workbook()
for d in range(1, monthrange(pre_mouth.year, pre_mouth.month)[1] + 1):
date_str = '{:04d}-{:02d}-{:02d}'.format(pre_mouth.year, pre_mouth.month, d)
daily_excel_path = os.path.join(excel_dir, 'idcard_{0}.xlsx'.format(date_str))
if not os.path.exists(daily_excel_path):
print('daily excel path not exists: {0}'.format(daily_excel_path))
continue
monthly_ws = monthly_wb.create_sheet(date_str)
daily_wb = load_workbook(daily_excel_path)
daily_ws = daily_wb.get_sheet_by_name('身份证')
for row in daily_ws.iter_rows(min_row=1, values_only=True):
monthly_ws.append(row)
monthly_excel_path = os.path.join(excel_dir, 'idcard_{0}.xlsx'.format(pre_mouth.strftime('%Y-%m')))
monthly_wb.remove(monthly_wb.get_sheet_by_name('Sheet'))
monthly_wb.save(monthly_excel_path)
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!