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 = '\n'.join(cell_value.split(' '))
                res.append(cell_value)
        return res

def newTextTable(data_row):
    head_row = ['Name', 'Standard\nSalary', 'Total\nProject\nBonus', 'Working\nHour', 'Effective\nWorking\nHour', '(A)Salary', '(B)Adjustment', '(C)Meal\nAllowance', '(D)Transportation\nAllowance', '(S=A+B+C+D)\nTotal\nIncome', 'Company\nPart', 'School\nPart']
    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 "\n\n", table.draw()

 

  

posted on 2016-11-12 17:18  姚灯灯!  阅读(2214)  评论(0编辑  收藏  举报

导航