【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.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 Roman'style = xlwt.XFStyle()style.font = fontinvoice = 'F:\pythonFile\\' + invoice_no + ' ' + invoice_namedata = xlrd.open_workbook(sample_file,formatting_info=True)new_excel = copy(data)ws = new_excel.get_sheet(0) # 獲取第一個sheetfirst_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 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 Roman'style = xlwt.XFStyle()style.font = fontinvoice = 'F:\pythonFile\\' + invoice_no + ' ' + invoice_namedata = xlrd.open_workbook(sample_file,formatting_info=True)new_excel = copy(data)ws = new_excel.get_sheet(0) # 獲取第一個sheetfirst_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()?
總結
以上是生活随笔為你收集整理的【Python实战】使用python批量生成发票的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Maven】Eclipse中的Mave
- 下一篇: php获取数组中的全部可以吗,php获取