【Python实战】使用python批量生成发票

一般的贸易或者货运型公司,经常需要做发票,有时候我们会遇到需要做大批量重复性的发票时,如果人工一个个去做,即耗时而且容易出错,这时我们可以用的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)

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

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

完整代码:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者:cacho_37967865
#博客:https://blog.csdn.net/sinat_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()

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Django是一个用于快速开发Web应用程序的Python Web框架。而python-docx-template是一个Python库,它可以使用Word文档作为模板,然后根据传入的数据批量生成Word文档。在Django中,我们可以利用python-docx-template库来实现批量生成Word文档的功能。 首先,我们需要在Django项目中安装python-docx-template库。可以使用pip命令来安装该库: ```bash pip install python-docx-template ``` 接下来,我们可以在Django项目中创建一个视图函数,用于接收数据并根据模板生成Word文档。在视图函数中,我们可以使用python-docx-template库提供的方法将数据填充到Word模板中,生成最终的Word文档。 例如,假设我们有一个Word文档模板`template.docx`,里面包含了一些需要填充数据的位置,我们可以在Django中这样写视图函数: ```python from docxtpl import DocxTemplate from django.http import HttpResponse def generate_word_document(request): # 从请求中获取数据 data = request.GET.get('data', '') # 读取Word模板 doc = DocxTemplate("template.docx") # 根据数据填充模板 context = {'data': data} doc.render(context) # 写入生成的Word文档 doc.save("generated_document.docx") # 返回生成的Word文档给用户 with open("generated_document.docx", 'rb') as f: response = HttpResponse(f.read(), content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document') response['Content-Disposition'] = 'attachment; filename=generated_document.docx' return response ``` 通过上述视图函数,我们可以在Django项目中实现批量生成Word文档的功能,用户可以通过传入数据来生成他们所需的Word文档。这样我们就可以方便地利用Python和Django来批量生成Word文档,提高生产效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值