Python3 利用xlrd 处理excel中的日期的格式

#!/usr/local/bin/python3
# -*- coding: utf8 -*-



import sys
from datetime import  date
from xlrd import open_workbook
from xlwt import Workbook
from xlrd import xldate_as_tuple


input_file = sys.argv[1]
output_file = sys.argv[2]

output_workbook = Workbook()
output_worksheet = output_workbook.add_sheet('jan_2013_output')

with open_workbook(input_file) as workbook:
    worksheet = workbook.sheet_by_name('January_2013')
    for row_index in range(worksheet.nrows):
        row_list_output = []
        for col_index in range(worksheet.ncols):
            if worksheet.cell_type(row_index, col_index) == 3:
                date_cell = xldate_as_tuple(worksheet.cell_value(row_index, col_index), workbook.datemode)
                date_cell = date(*date_cell[0:3]).strftime('%m-%d--%Y')
                #print("{33}")
                #print(date_cell)
                row_list_output.append(date_cell)
                output_worksheet.write(row_index, col_index, date_cell)
            else:

                non_date_cell = worksheet.cell_value(row_index,col_index)
                row_list_output.append(non_date_cell)
                #print(non_date_cell)
                output_worksheet.write(row_index,col_index,non_date_cell)

output_workbook.save(output_file)

你可能感兴趣的:(python)