制作一个网站平台需要多少钱,小公司企业简介300字,wordpress自动水印,互联网服务平台单位机动车二维码一般的贸易或者货运型公司#xff0c;经常需要做发票#xff0c;有时候我们会遇到需要做大批量重复性的发票时#xff0c;如果人工一个个去做#xff0c;即耗时而且容易出错#xff0c;这时我们可以用的python和excel相关的模块去批量生成。
现在有这样一个场景#xff…一般的贸易或者货运型公司经常需要做发票有时候我们会遇到需要做大批量重复性的发票时如果人工一个个去做即耗时而且容易出错这时我们可以用的python和excel相关的模块去批量生成。
现在有这样一个场景有个excel的货运发票模板invoice GIL.xls然后需要做多个发票名称不一样收货人不一样的多个发票。这时我们可以定义新的发票名称为 invoice_noGIL.xlsinvoice_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.nrowsnum 0data []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 1row.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 Truefont.name Times New Romanstyle xlwt.XFStyle()style.font fontinvoice F:\pythonFile\\ invoice_no invoice_namedata xlrd.open_workbook(sample_file,formatting_infoTrue)new_excel copy(data)ws new_excel.get_sheet(0) # 获取第一个sheetfirst_col ws.col(0) # 第一列first_col.widthwidth # 第一列宽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_infoTrue
此外有width、font、height、bold、style等等关于excel字体单元格信息等配置
最后做发票就可以了
def mk_invoices():packing_file F:\pythonFile\\packing.xlsxdata 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 copysample_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.nrowsnum 0data []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 1row.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 Truefont.name Times New Romanstyle xlwt.XFStyle()style.font fontinvoice F:\pythonFile\\ invoice_no invoice_namedata xlrd.open_workbook(sample_file,formatting_infoTrue)new_excel copy(data)ws new_excel.get_sheet(0) # 获取第一个sheetfirst_col ws.col(0) # 第一列first_col.widthwidth # 第一列宽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.xlsxdata 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()