Python脚本将Excel日期格式从mmm-yy转换为yyyy-mm-dd?

时间:2016-08-05 06:15:57

标签: python excel

嗨,有人可以帮忙。

我希望修改Excel工作簿第3页上的A列,以便更改日期格式:

Sep-15至2016-09-15。

在Excel中,格式为mmm-yy我希望将其更改为yyyy-mm-dd。

试着理解它,我知道你可以使用像pandas或xlsxwriter这样的模块,但这个例子没有意义。

由于

1 个答案:

答案 0 :(得分:1)

见这个例子。

import xlsxwriter

workbook = xlsxwriter.Workbook('date_examples.xlsx')
worksheet = workbook.add_worksheet()

# Widen column A for extra visibility.
worksheet.set_column('A:A', 30)

# A number to convert to a date.
number = 41333.5

# Write it as a number without formatting.
worksheet.write('A1', number)                # 41333.5

format2 = workbook.add_format({'num_format': 'dd/mm/yy'})
worksheet.write('A2', number, format2)       # 28/02/13

format3 = workbook.add_format({'num_format': 'mm/dd/yy'})
worksheet.write('A3', number, format3)       # 02/28/13

format4 = workbook.add_format({'num_format': 'd-m-yyyy'})
worksheet.write('A4', number, format4)       # 28-2-2013

format5 = workbook.add_format({'num_format': 'dd/mm/yy hh:mm'})
worksheet.write('A5', number, format5)       # 28/02/13 12:00

format6 = workbook.add_format({'num_format': 'd mmm yyyy'})
worksheet.write('A6', number, format6)       # 28 Feb 2013

format7 = workbook.add_format({'num_format': 'mmm d yyyy hh:mm AM/PM'})
worksheet.write('A7', number, format7)       # Feb 28 2013 12:00 PM

workbook.close()

Source