需求:财务部门每月都会导出上个月每个加盟商的往来出入库明细,加盟商在DRP系统里大概有200+,通过人工的方法,一个一个查询导出的话,效率低、耗时长。最后工具搞定后只需要三分钟就搞定了所有的报表导出,实用性很高。

解决方案:考虑到这问题后我就在思考思路,确定好流程,之前看过北京理工大学的教授嵩天老师的Python课程,讲到程序的设计技巧就是IPO,输入、处理、输出三部分。顺着IPO思路就很清晰了。

输入:加盟商编码、上个月月初第一天、上个月月末最后一天

处理:从数据库中查询出结果以列表的形式保存,遍历列表写入excel文件

输出:保存到指定文件夹的excel表

先看下效果图:

打包后exe可执行程序

双击后开始运行下载:

运行窗口

文件保存目录:

文件导出后存放目录结构

excel内容示例:

导出文件内容示例

开发环境:

Win7_64+Pycharm+Python3.5+SQLServer2008

打包工具:

Pyinstaller第三方工具

pyinstaller -F autoexport.py

涉及到:

数据库的存储过程调用、excel写入第三方库xlwt、元组列表转换、None处理、日期函数转字符串、for循环遍历、文件创建、字符串拼接等等

PS:基础知识真的很重要。

详细见代码注释:(代码没review写的糙,没有进一步优化)

拓展:后期可以拓展成可视化界面

代码如下:

# -*- coding:utf-8 -*-

# __author__ = 'codingme'

import pymssql

import os

import xlwt

from datetimeimport datetime

import time

import calendar

# 获取数据库连接

def get_conn():

server ='ip:port'

datebase ='database_name'

user ='user'

password ='password'

conn= pymssql.connect(server, user, password, datebase)

return conn

# 获取加盟商代码和名称

def get_merchant_list(conn):

# M5030'[M|L|K]5%'

select_merchant_sql ="select merchantid, shutname from j_merchant where merchantid like '[M|L|K]5%' "

cursor = conn.cursor()

cursor.execute(select_merchant_sql)

return cursor.fetchall()

# 获取加盟商时间段内出入库明细

def get_merchant_detail(conn, merchant_id, beg_date, end_date):

# SQLSERVER写好的查询存储过程

select_merchant_flow_sql ='XXXXXXXXXXX'

cursor = conn.cursor()

# 调用存储过程

cursor.callproc(select_merchant_flow_sql, (merchant_id, beg_date, end_date))

# 结果集转换成list

return list(cursor)

# EXCEL导出标题样式

def title_style():

style = xlwt.XFStyle()# 初始化样式

font = xlwt.Font()# 为样式创建字体

font.name ='Times New Roman'

font.bold =True

font.color_index =4

font.height =500

style.font = font

# style.borders = borders

al = xlwt.Alignment()

al.horz = xlwt.Alignment.HORZ_CENTER

al.vert = xlwt.Alignment.VERT_CENTER

style.alignment = al

return style

# excel表格单元格格式

def set_style(name, height, bold=False):

style = xlwt.XFStyle()# 初始化样式

font = xlwt.Font()# 为样式创建字体

font.name = name# 'Times New Roman'

font.bold = bold

font.color_index =4

font.height = height

# borders= xlwt.Borders()

# borders.left= 6

# borders.right= 6

# borders.top= 6

# borders.bottom= 6

style.font = font

# style.borders = borders

al = xlwt.Alignment()

al.horz = xlwt.Alignment.HORZ_LEFT

style.alignment = al

return style

# 加盟商对账单写入excel表格

def write_excel_merchant(merchant_id, merchant_name, cursor):

# 新建目录:运行目录+当前日期拼接新目录

dir_name = os.path.join(os.getcwd(), time.strftime("%Y-%m-%d", time.gmtime()) +'出入库明细单')

if not os.path.exists(dir_name):

os.makedirs(dir_name)

file = xlwt.Workbook()# 创建工作簿

'''

创建第一个sheet:

sheet1

'''

sheet1 = file.add_sheet(u'sheet1', cell_overwrite_ok=True)

# 创建sheet表头

sheet1.write_merge(0, 3, 0, 9, 'XXXX(深圳)有限公司加盟商出入库明细单', title_style())

sheet1.write(4, 0, '加盟商名称', set_style('Times New Roman', 220, True))

sheet1.write(4, 1, merchant_name + merchant_id, set_style('Times New Roman', 220, True))

sheet1.write(5, 0, '统计日期', set_style('Times New Roman', 220, True))

sheet1.write(5, 1, get_first_day()+'至'+get_last_day(), set_style('Times New Roman', 220, True))

sheet1.write(5, 2, '打印日期', set_style('Times New Roman', 220, True))

sheet1.write(5, 3, time_format(time.gmtime()), set_style('Times New Roman', 220, True))

# 生成表头第6行标题

row0 = [u'单据类型', u'单据编号', u'销售类型', u'退货率', u'发货地编号', u'发货地名称', u'收货组织', u'收货组织名称',\

u'收货地编号', u'收货地名称', u'发货日期', u'年份', u'季节', u'款式名称', u'款号', u'颜色', u'尺码', u'发货折扣',\

u'发货数量', u'发货金额', u'备注']

for xin range(0, len(row0)):

sheet1.write(6, x, row0[x], set_style('Times New Roman', 220, True))

# 设置行宽度

if xin [1, 5, 9, 13, 20]:

sheet1.col(x).width =256 *25

else:

sheet1.col(x).width =256 *12

# 从第七行开始遍历excel写入

i =7

# for循环写入存储过程返回值:加盟商出入库明细列表

for rowin cursor:

for yin range(0, len(row0)):

# None值转换成0处理,然后转换成字符串

sheet1.write(i, y, str(none_to_zero(row[y])))

# 移动到下一行继续循环

i +=1

# 保存文件名拼接: 结算方代码_结算方名称_出入库明细_日期年月.xls

file_name =str(merchant_id).strip() +'_' + merchant_name +'_' +'出入库明细单' + get_first_day()[0:7] +'.xls'

# 保存excel,目录+文件名

file.save(os.path.join(dir_name, file_name))

print(file_name +'已经导出完成')

# NoneType转换成0

def none_to_zero(none_type):

if none_typeis None:

return 0

else:

return none_type

# 获取当前日期字符串格式

def time_format(in_time):

times = time.strftime("%Y-%m-%d", in_time)

return times

# 上个月最后一天

def get_last_day():

d = datetime.now()

c = calendar.Calendar()

year = d.year

month = d.month

if month ==1:

month =12

year -=1

else:

month -=1

days = calendar.monthrange(year, month)[1]

return datetime(year, month, days).strftime('%Y-%m-%d')

# 上个月第一天

def get_first_day():

d = datetime.now()

c = calendar.Calendar()

year = d.year

month = d.month

if month ==1:

month =12

year -=1

else:

month -=1

return datetime(year, month, 1).strftime('%Y-%m-%d')

# 程序运行入口

if __name__ =='__main__':

conn = get_conn()

merchant_list = get_merchant_list(conn)

first_day = get_first_day()

last_day = get_last_day()

start = time.time()

print('开始导出所有加盟商出入库明细==========================')

for merchantin merchant_list:

merchant_id = merchant[0]

merchant_name = merchant[1]

cursor = get_merchant_detail(conn, merchant_id, first_day, last_day)

if len(cursor) ==0:

continue

else:

write_excel_merchant(merchant_id, merchant_name, cursor)

end = time.time() - start

print(last_day +'所有加盟商出入库明细已经下载完成 %s' % end)

镇楼:人生苦短,我用Python

python出入库_Python-批量导出excel加盟商出入库明细相关推荐

  1. python从mysql导出大量数据_python批量导出导入MySQL用户的方法

    数据库迁移(A -> B),需要把用户也迁移过去,而用户表(mysql.user)有上百个用户.有2种方法进行快速迁移: 1,在同版本的条件下,直接备份A服务器的mysql数据库,还原到B服务器 ...

  2. POI批量导出Excel ZIP打包下载

    POI批量导出Excel ZIP打包下载 1.公共抽象导出Excel类 需要自己实现两个抽象方法: getColumValueForColunmName : 扩展方法:根据名称判断来做值得转换 比如: ...

  3. VBA应用笔记 -- 批量导出excel工作表中的图片

    业务场景: 日常工作中,我们可能会遇到需要批量导出excel表中的图片的情况,按照网友的做法,批量导出excel中的图片主要有几种方法: 解压缩方法:可以通过将excel文件转成rar压缩文件,解压后 ...

  4. Java POI 导出EXCEL经典实现 Java导出Excel弹出下载框

    原文转载:http://blog.csdn.net/evangel_z/article/details/7332535 目录(?)[+] 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数 ...

  5. 使用POI批量导出Excel文件(SSM)

    文章目录 前言 如何使用POI批量导出Excel文件(SSM) 一.什么是POI? 模块 二.使用步骤 1.引入依赖 2.mapper层代码 包括Mapper接口.Mapper SQL代码 Mappe ...

  6. python批量处理excel数据_Python批量处理Excel,真香(超实用!)

    本文介绍了利用Python批量处理Excel文件的一种方法,超实用,超简单.轻松可实现,节省时间不只一点点.文章不长,功能超强. 上菜. 某一天,老板丢个我一个任务.需要将400多张表按照一定条件进行 ...

  7. python如何对excel批量加密_Python批量处理Excel,真香(超实用!)

    本文介绍了利用Python批量处理Excel文件的一种方法,超实用,超简单.轻松可实现,节省时间不只一点点.文章不长,功能超强. 上菜. 某一天,老板丢个我一个任务.需要将400多张表按照一定条件进行 ...

  8. python批量处理excel文本改为数字_Python批量修改Excel中的文件内容

    import os import xlrd from xlutils.copy import copy def base_dir(filename=None): return os.path.join ...

  9. python自动翻译excel某一列_python批量将excel内容进行翻译写入功能

    由于小编初来乍到,有很多地方不是很到位,还请见谅,但是很实用的哦! 1.首先是需要进行文件的读写操作,需要获取文件路径,方式使用os.listdir(路径)进行批量查找文件. file_path = ...

  10. 压缩包套压缩包的形式批量导出excel

    1.背景: 实际应用中,批量导出exel的功能,用户为了方便归类整理,要求导出一个压缩包,然后压缩包里再按某种规则进行压缩打包excel.最终导出到本地环境的文件是一个压缩包套压缩包的形式. 2.原理 ...

最新文章

  1. 提取文件出错_提取中文、英文和数字,其实很简单
  2. python使用redis第一节、环境配置
  3. CentOS7查看开放端口命令
  4. MySQL 事务(Transaction)篇
  5. 【STM32】【STM32CubeMX】STM32CubeMX的使用之五:定时器时基配置及其中断
  6. 【ElasticSearch】Es 源码之 AsyncSearchMaintenanceService 源码解读
  7. destoon 自定义session丢失
  8. 小米崔宝秋:一家互联网公司没有信息安全团队,就像在“裸奔”!
  9. 数学基础加强2---概率论与贝叶斯先验
  10. WebSocket心跳检测和重连机制
  11. 支持HTTP2的cURL——基于Alpine的最小化Docker镜像
  12. 20200601每日一句
  13. 2.8数据-paddlepaddle数据集uci_housing
  14. 阿里云ACE 架构师 认证指南
  15. 艾永亮:B站破壁出圈,同是弹幕视频网站,为什么倒下的是A站?
  16. 蓝牙技术|蓝牙(BLE)低功耗你所不了解的特性
  17. 实测超轻量中文OCR开源项目,总模型仅17M
  18. JavaWeb基础系列(八)商城后台增删改查
  19. 二维C语言,二维FFT,IFFT,c语言实现
  20. 5-14 电话聊天狂人 (25分)

热门文章

  1. ppt菜鸟学飞第一天——基础知识及字体知识
  2. ut红种状态解决办法汇总
  3. 神经网络——基础思想
  4. CleanMyMac X下载Mac系统清理优化工具
  5. android游戏录音,音频录音剪辑软件 1.1.15 安卓版
  6. 欢迎使用传真服务器系统,coFax传真服务器 OCR页面号码识别传真
  7. 如何通过视频转换器将qsv格式转换成mp4格式
  8. springboot实现上传图片添加水印
  9. 90后的青春,定格在被淡忘的QQ空间里
  10. HTML静态网页作业——仿天猫购物商城(7页) 网页设计作业,网页制作作业, 学生网页作业, 网页作业成品, 网页作业模板