|
@@ -0,0 +1,102 @@
|
|
|
+"""导出车辆低效明细
|
|
|
+"""
|
|
|
+import xlsxwriter
|
|
|
+from datetime import datetime
|
|
|
+from dateutil.relativedelta import relativedelta
|
|
|
+from loguru import logger
|
|
|
+import psycopg
|
|
|
+
|
|
|
+# 数据库连接信息
|
|
|
+db_host = "172.16.107.5" # 数据库主机地址
|
|
|
+db_port = 5432 # 数据库端口号
|
|
|
+db_username = "finance" # 数据库用户名
|
|
|
+db_password = "Finance@unicom23" # 数据库密码
|
|
|
+dbname = "financialdb" # 数据库名称
|
|
|
+conn_info = f"host='{db_host}' port={db_port} user='{db_username}' password='{db_password}' dbname='{dbname}'"
|
|
|
+# 获取当前日期,并计算上个月的第一天
|
|
|
+today = datetime.today()
|
|
|
+start_date = today - relativedelta(months=1, day=1)
|
|
|
+year_month = start_date.strftime('%Y%m')
|
|
|
+# 输出文件路径
|
|
|
+output_path = f"{year_month}低效车辆明细.xlsx"
|
|
|
+
|
|
|
+with psycopg.connect(
|
|
|
+ conninfo=conn_info,
|
|
|
+ row_factory=psycopg.rows.dict_row
|
|
|
+) as conn:
|
|
|
+ with conn.cursor() as curs:
|
|
|
+ sql = f"""
|
|
|
+select che_pai_hao,
|
|
|
+ first_unit,
|
|
|
+ second_unit,
|
|
|
+ third_unit,
|
|
|
+ che_liang_suo_shu_dan_wei,
|
|
|
+ che_liang_lai_yuan,
|
|
|
+ che_liang_lei_xing,
|
|
|
+ che_liang_shi_yong_xing_zhi,
|
|
|
+ round(ri_jun_li_cheng_sum, 2) as ri_jun_li_cheng_sum,
|
|
|
+ round(chu_qin_lv_sum, 4) as chu_qin_lv_sum,
|
|
|
+ round(zong_li_cheng_sum, 2) as zong_li_cheng_sum,
|
|
|
+ xing_shi_tian_shu_sum as xing_shi_tian_shu_sum,
|
|
|
+ workday_sum as workday_sum
|
|
|
+from car.car_di_xiao_month
|
|
|
+where year_month = {year_month}
|
|
|
+order by first_unit,
|
|
|
+ second_unit,
|
|
|
+ third_unit,
|
|
|
+ che_pai_hao
|
|
|
+ """
|
|
|
+ logger.info(f"sql: {sql}")
|
|
|
+ curs.execute(sql)
|
|
|
+ l = curs.fetchall()
|
|
|
+ workbook = xlsxwriter.Workbook(output_path)
|
|
|
+ worksheet = workbook.add_worksheet()
|
|
|
+ # 定义单元格格式
|
|
|
+ percent_format = workbook.add_format({'num_format': '0.00%'}) # 百分比格式
|
|
|
+ number_format = workbook.add_format({'num_format': '#,##0.00'}) # 数字格式(千分位 + 两位小数)
|
|
|
+ # 写入表头
|
|
|
+ headers = ['车牌号', '单位', '二级', '三级', '车辆所属单位', '车辆来源', '车辆类型', '车辆使用性质', '日均里程',
|
|
|
+ '出勤率', '总里程', '行驶天数', '工作日天数']
|
|
|
+ worksheet.write_row(0, 0, headers)
|
|
|
+ for row_num, item in enumerate(l):
|
|
|
+ col = 0
|
|
|
+ worksheet.write(row_num + 1, col, item.get('che_pai_hao'))
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('first_unit'))
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('second_unit'))
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('third_unit'))
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('che_liang_suo_shu_dan_wei'))
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('che_liang_lai_yuan'))
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('che_liang_lei_xing'))
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('che_liang_shi_yong_xing_zhi'))
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('ri_jun_li_cheng_sum'), number_format)
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('chu_qin_lv_sum'), percent_format)
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('zong_li_cheng_sum'), number_format)
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('xing_shi_tian_shu_sum'))
|
|
|
+ col += 1
|
|
|
+ worksheet.write(row_num + 1, col, item.get('workday_sum'))
|
|
|
+ worksheet.set_column('A:A', 12)
|
|
|
+ worksheet.set_column('B:B', 12)
|
|
|
+ worksheet.set_column('C:C', 12)
|
|
|
+ worksheet.set_column('D:D', 12)
|
|
|
+ worksheet.set_column('E:E', 12)
|
|
|
+ worksheet.set_column('F:F', 12)
|
|
|
+ worksheet.set_column('G:G', 12)
|
|
|
+ worksheet.set_column('H:H', 12)
|
|
|
+ worksheet.set_column('I:I', 12)
|
|
|
+ worksheet.set_column('J:J', 12)
|
|
|
+ worksheet.set_column('K:K', 12)
|
|
|
+ worksheet.set_column('L:L', 12)
|
|
|
+ worksheet.set_column('M:M', 12)
|
|
|
+ # 保存并关闭工作簿
|
|
|
+ workbook.close()
|