使用python處理Excel,Excel中一行數據生產一個Excel文件


應用場景:比如說收到一個文件,文件里面有幾百個用戶,需要按照一定的格式每一個用戶生成一個Excel文件

 

需要生成這樣結果:

 

每個文件格式如下:

 

 

代碼如下:

# -*- coding: utf-8 -*-
"""
Created on Tue Apr 28 15:16:14 2020

@author: Admin
"""
#導入模塊
import pandas as pd
import numpy as np
import xlrd  #讀Excel
import xlwt  #將內容寫進Excel

#打開excel文件1  
workbook = xlrd.open_workbook(r'F:\\python\\test\\bb_6c.xlsx')   
sheet = workbook.sheet_by_index(0)  
rows = [sheet.row_values(row,0,14) for row in range(sheet.nrows)]
name_lists = {}   
for r in rows:
    #因為我們是根據姓名來切分,姓名在第二列。所以這里是r[1]
    if r[1] not in name_lists:
        name_lists[r[1]] = []
    name_lists[r[1]].append(r)

#打開Excel文件2
workbook =xlrd.open_workbook(r'F:\\python\\test\\bb_6p.xlsx')
sheet1= workbook.sheet_by_index(0)   
rows1 = [sheet1.row_values(row,0,12) for row in range(sheet1.nrows)]
name_lists1 = {}   
for r in rows1:
    #因為我們是根據姓名來切分,姓名在第二列。所以這里是r[0]
    if r[0] not in name_lists1:
        name_lists1[r[0]] = []
    name_lists1[r[0]].append(r)

#寫進Excel里面
for k, k2 in zip(name_lists,name_lists1):
    wb = xlwt.Workbook()
    #新建sheet
    ws = wb.add_sheet(k)
    #設置列寬度 ,其中第一列要寬一些 ,其他的小一些
    ws.col(0).width=8000
    for i in range(1,14):
        ws.col(i).width=3333
    
    #設置加粗
    font = xlwt.Font()
    font.bold = True  #tyle_ziti = xlwt.easyxf('font: bold on')
    #設置背景顏色黃色
    pattern = xlwt.Pattern() # Create the Pattern
    pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
    pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
    
    
    #設置居中 
    alignment = xlwt.Alignment() # Create Alignment
    alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
    alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
    
    #設置邊框
    borders = xlwt.Borders() # Create Borders
    borders.left = xlwt.Borders.THIN 

    
    # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
    borders.right = xlwt.Borders.THIN
    borders.top = xlwt.Borders.THIN
    borders.bottom = xlwt.Borders.THIN
    borders.left_colour = 0x08
    borders.right_colour = 0x08
    borders.top_colour = 0x08
    borders.bottom_colour = 0x08
    
    style1 = xlwt.XFStyle()
    style2 = xlwt.XFStyle()
    style3 = xlwt.XFStyle()
    style4 = xlwt.XFStyle()
    style5 = xlwt.XFStyle()
    style7 = xlwt.XFStyle()

 
    
    #1.邊框,居中 加粗
    style1.borders=borders
    style1.alignment=alignment
    style1.font=font
    # 2.邊框,居中   
    style2.borders=borders
    style2.alignment=alignment
    #3.邊框,居中,黃色
    style3.borders=borders
    style3.alignment=alignment 
    style3.pattern=pattern

    #4.邊框,居中 加粗,黃色
    style4.borders=borders
    style4.alignment=alignment 
    style4.pattern=pattern
    style4.font=font
    
    
    #5,邊框
    style5.borders=borders
    
    #
    style7.borders=borders
    style7.alignment=alignment
    style7.num_format_str="0.00%"
    
    #6.合並,換行,底部對齊,設置行高
    style6 = xlwt.XFStyle()
    tall_style = xlwt.easyxf('font:height 1200;') # 36pt,類型小初的字號
    first_row = ws.row(3)
    first_row.set_style(tall_style)
    alignment1 = xlwt.Alignment()
    alignment1.horz = xlwt.Alignment.HORZ_LEFT
    alignment1.vert = xlwt.Alignment.VERT_BOTTOM
    style6.alignment=alignment1
    style6.alignment.wrap = 1
    
    #設置加粗並設置黃底
    #style_ziti_h = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on')
    #設置黃底
    #style_h = xlwt.easyxf('pattern: pattern solid, fore_colour yellow')
    # write_merge函數參數 start_row,end_row,start_col,end_col 
    
    #這里先寫入表頭
    ws.write_merge(0, 0, 0, 13, '欠款明細',style1)   #需要合並write_merge(0, 0, 0, 13,style_ziti),還需要加粗
    ws.write(1, 0, '合同號',style1)    #需要加粗
    ws.write(1, 1, '姓名',style1)
    ws.write(1, 2, '貸款本金',style1)
    ws.write(1, 3, '首次還款日',style1)
    ws.write(1, 4, '分期期數',style1)
    ws.write(1, 5, '已還期數',style1)
    ws.write(1, 6, '未還期數',style1)
    ws.write(1, 7, '每期期款',style1)
    ws.write(1, 8, '未還本金',style1)
    ws.write(1, 9, '月貸款利率%',style1)
    ws.write(1, 10, '未還利息',style1)
    ws.write(1, 11, '未還本息總和',style1)
    ws.write(1, 12, '利息損失',style1)
    ws.write(1, 13, '欠款總額',style1)  #第二行的都需要加粗
    row_idx = 2          
    for i in name_lists[k]:
        col_idx=0
        for j in i :
            ws.write(row_idx,col_idx,j,style3)  #需要黃體
            col_idx=col_idx+1
        row_idx=row_idx+1
        
    ws.write_merge(3, 3,0,13,'欠款計算說明:\n欠款總額=未還本息總和+利息損失;\n未還本息總和=等額本息還款法計算出的每期期款金額*未還期數;\n利息損失=以代償金額(即應還未還的貸款本息和)為基數,按照銀行同期貸款利率4.35%的標准,自完成代償之日起,計算至實際清償之日止。'    ,style6 )
    ws.write_merge(4, 4, 0, 3, '附:等額本息還款法計算公式:',style5)  #需要合並write_merge(7, 0, 0, 3)
    ws.write_merge(5, 5, 0, 3, '輸入區',style5) #需要合並write_merge(8, 0, 0, 3)
    ws.write(6, 0, '本金',style2)
    ws.write(6, 1, '分期數(月)',style2)
    ws.write(6, 2, '月利率',style2)
    ws.write(6, 3, '年化',style2)  
    ws.write(7, 0, xlwt.Formula('c3'),style2)
    ws.write(7, 1, xlwt.Formula('e3'),style2)
    ws.write(7, 2, xlwt.Formula('j3'),style7)
    ws.write(7, 3, xlwt.Formula('j3*12'),style7)   
    ws.write(9, 0, '合同號',style4)
    ws.write(9, 1, '期數',style4)   #第13行需要黃體加粗
    ws.write(9, 2, '每月還款額',style4)
    ws.write(9, 3, '本月應還本金',style4)
    ws.write(9, 4, '本月應還利息',style4)
    ws.write(9, 5, '已還本息',style4)
    ws.write(9, 6, '未還本息',style4)
    ws.write(9, 7, '應還款日',style4)
    ws.write(9, 8, '完成代償日',style4)
    ws.write(9, 9, '利息暫計至',style4)
    ws.write(9, 10, '計息天數',style4)
    ws.write(9, 11, '利息損失',style4)
    
    row_bb=10    
    for gg in name_lists1[k2]:
        col_bb=0
        for hh in gg:            
            ws.write(row_bb,col_bb,hh,style2)
            col_bb=col_bb+1
        row_bb=row_bb+1    
        
    ws.write_merge(16, 16, 0, 5, '未還本息合計',style4) #需要合並write_merge(16, 0, 0, 4)
    ws.write(16, 6, xlwt.Formula('l3'),style4)
    ws.write_merge(16, 16,7,10, '利息損失小計',style4)
    ws.write(16, 11, xlwt.Formula('m3'),style4)
    wb.save('F:\\python\\test\\欠款明細-'+k+'.xls')  #如果要固定存在某個文件夾,則可以這樣save(r'f:\python_to_excel_xlwt\cell_width.xls')
    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM