zoukankan      html  css  js  c++  java
  • python读取excel并制表输出

    源码如下:

    #!/usr/bin/python
    #coding=UTF-8
     
    import xlrd
    import sys
    from texttable import Texttable
     
    def sheetRowToSlice(sheet, r, colnum):
            res = []
            for c in range(colnum):
                    cell_value = sheet.cell(r, c).value
                  #  if isinstance(cell_value, unicode):
                   #         cell_value = '
    '.join(cell_value.split(' '))
                    res.append(cell_value)
            return res
    
    def newTextTable(data_row):
        head_row = ['Name', 'Standard
    Salary', 'Total
    Project
    Bonus', 'Working
    Hour', 'Effective
    Working
    Hour', '(A)Salary', '(B)Adjustment', '(C)Meal
    Allowance', '(D)Transportation
    Allowance', '(S=A+B+C+D)
    Total
    Income', 'Company
    Part', 'School
    Part']
        table = Texttable(200)
        table.set_cols_dtype(['t', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'])
        table.set_cols_align(["c", "c", "c",  "c", "c", "c", "c", "c", "c", "c", "c", "c"])
        table.set_deco(Texttable.HEADER)
        table.add_rows([head_row, data_row])
        return table
        
     
    data = xlrd.open_workbook("test.xlsx")
     
    sheet = data.sheets()[0]
     
    nrows_num = sheet.nrows
     
    ncols_num = sheet.ncols
    
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    
    #data_row = ['姚增增', 3000.00, 1200.00, 168.00, 140.00, 3700.00, 520.00, 291.67, 0.00, 4511.67, 2255.83, 2255.83]
    for r in range(nrows_num):
        cell_value = str(sheet.cell(r, 0).value)
        if "Name" in cell_value:
            start = r
        if '合计' in cell_value:
            end = r
    
    for r in range(start + 1, end):
        data_row = sheetRowToSlice(sheet, r, ncols_num)
        table = newTextTable(data_row)
        print "
    
    ", table.draw()

      

  • 相关阅读:
    Rocket
    Rocket
    Rocket
    Rocket
    Scala
    Rocket
    Rocket
    Rocket
    Rocket
    Rocket
  • 原文地址:https://www.cnblogs.com/YaoDD/p/6057098.html
Copyright © 2011-2022 走看看