一般的贸易或者货运型公司,经常需要做发票,有时候我们会遇到需要做大批量重复性的发票时,如果人工一个个去做,即耗时而且容易出错,这时我们可以用的python和excel相关的模块去批量生成。

现在有这样一个场景,有个excel的货运发票模板invoice GIL.xls,然后需要做多个发票名称不一样,收货人不一样的多个发票。这时我们可以定义新的发票名称为 invoice_no+GIL.xls(invoice_no为变量);发票内部需要更新 invoice_no、name两个变量

所以我们可以定义两个函数,一个是获取invoice_no、name的函数 get_excel_data(packing_file),通过excel文件packing_file获取。很简单的,就是通过xlrd模块获取 invoice_no、name放置在一个二维列表中(我们也可以其他方式获取)

# 获取发票需要的动态参数:第3行第2列发票号invoice,第3行第8列姓名name
def get_excel_data(packing_file):
    data = xlrd.open_workbook(packing_file)
    table = data.sheets()[0]
    nrows = table.nrows
    num = 0
    data = []
    for i in range(2, nrows):
        row = []
        invoice_no = table.cell_value(i, 1)
        name = table.cell_value(i, 7)
        if invoice_no !='':
            num = num +1
            row.append(invoice_no)
            row.append(name)
            data.append(row)
    print('总发票数量:',num)
    return data

另外需要一个函数 去更新发票模板中对应位置的nvoice_no、name

def make_excel(invoice_no,name):
    width = 256 * 8  # 8个字符宽
    # 字体和格式
    font = xlwt.Font()
    font.height = 240  # 12号字体
    font.bold = True
    font.name = 'Times New Roman'
    style = xlwt.XFStyle()
    style.font = font
    invoice = 'F:\pythonFile\\' + invoice_no + ' ' + invoice_name
    data = xlrd.open_workbook(sample_file,formatting_info=True)
    new_excel = copy(data)
    ws = new_excel.get_sheet(0)      # 获取第一个sheet
    first_col = ws.col(0)            # 第一列
    first_col.width=width            # 第一列宽
    ws.write(7, 1, name,style)             # B8(7,1)
    ws.write(8, 5, invoice_no,style)       # F9(8,5)
    new_excel.save(invoice)

这里用到了xlutils模块,复制一份发票模板的数据,注意下面的一个参数,表示全部复制模板的信息(包括样式)

formatting_info=True


此外有width、font、height、bold、style等等关于excel字体,单元格信息等配置

最后做发票就可以了

def mk_invoices():
    packing_file = 'F:\pythonFile\\packing.xlsx'
    data = get_excel_data(packing_file)      # 获取所需参数
    for info in data:
        invoice_no = info[0]
        name = info[1]
        print(invoice_no,name)
        make_excel(invoice_no, name)

大多数时候发票里面的内容非常复杂,各种格式都有,执行会报错

python批量开票 批量开票导入模板_ico

这个时候需要对UnicodeUtils.py 进行修改一下就可以了,其中黄色部分是旧的

python批量开票 批量开票导入模板_ico_02

完整代码:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者:cacho_37967865
#博客:
#文件:pymysqlModel.py
#日期:2018-10-22
#备注:pip install pymysql  pymysql是Python中操作MySQL的模块   F:\python_env\PaChong_env
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import xlrd
import xlwt
from xlutils.copy import copy

sample_file = 'F:\pythonFile\\invoice GIL.xls'
invoice_name = 'GIL.xls'


# 发票模板:需要修改的地方(动态)
def invoice():
    data = xlrd.open_workbook(sample_file)
    table = data.sheets()[0]
    a = table.cell_value(7, 1)
    b = table.cell_value(8, 5)
    print(a,b)


# 获取发票需要的动态参数:第3行第2列发票号invoice,第3行第8列姓名name
def get_excel_data(packing_file):
    data = xlrd.open_workbook(packing_file)
    table = data.sheets()[0]
    nrows = table.nrows
    num = 0
    data = []
    for i in range(2, nrows):
        row = []
        invoice_no = table.cell_value(i, 1)
        name = table.cell_value(i, 7)
        if invoice_no !='':
            num = num +1
            row.append(invoice_no)
            row.append(name)
            data.append(row)
    print('总发票数量:',num)
    return data


# TypeError: descriptor 'decode' requires a 'bytes' object but received a 'NoneType'
# F:\python_env\PaChong_env\lib\site-packages\\xlwt\UnicodeUtils.py
def make_excel(invoice_no,name):
    width = 256 * 8  # 8个字符宽
    # 字体和格式
    font = xlwt.Font()
    font.height = 240  # 12号字体
    font.bold = True
    font.name = 'Times New Roman'
    style = xlwt.XFStyle()
    style.font = font
    invoice = 'F:\pythonFile\\' + invoice_no + ' ' + invoice_name
    data = xlrd.open_workbook(sample_file,formatting_info=True)
    new_excel = copy(data)
    ws = new_excel.get_sheet(0)      # 获取第一个sheet
    first_col = ws.col(0)            # 第一列
    first_col.width=width            # 第一列宽
    ws.write(7, 1, name,style)             # B8(7,1)
    ws.write(8, 5, invoice_no,style)       # F9(8,5)
    new_excel.save(invoice)


def mk_invoices():
    packing_file = 'F:\pythonFile\\packing.xlsx'
    data = get_excel_data(packing_file)      # 获取所需参数
    for info in data:
        invoice_no = info[0]
        name = info[1]
        print(invoice_no,name)
        make_excel(invoice_no, name)


if __name__ == '__main__':
    mk_invoices()