我正在尝试创建一个标记日期和日期名称的Python脚本。我已经弄清了大部分内容,确保工作日总是在同一个excel专栏(这是我想做的)。显然,我希望这个脚本可以在任何一年中进行扩展,以正确生成工作日和日期,但是我无法弄清楚如何在脚本达到本月分配的最大天数后停止运行。
现在它已经在1月份被硬编码了,但是一旦我弄清楚如何正确地停止日期生成器,我打算让它运行整整几个月。
import calendar
import datetime
import xlsxwriter
import csv
Year = raw_input("Enter the year do you need a spreadsheet for: ")
print "you entered", Year
Year = int(Year)
workbook = xlsxwriter.Workbook('spreadsheetsgt.xlsx')
worksheet = workbook.add_worksheet()
#How many days in a month
#always start with month 1
startMonth = 1
getMonthDays = calendar.monthrange(Year,startMonth)
totalMonthDays = max(getMonthDays)
#get total days for the month
row = 0
#Define formats
date_format = workbook.add_format({'num_format': 'd-mmm-yy'})
cell_format = workbook.add_format({'bold': True, 'italic': True})
Month = 1
for i in range (0, totalMonthDays):
getMonthDays = calendar.monthrange(Year,startMonth)
totalMonthDays = max(getMonthDays)
dayOfWeek = calendar.weekday(Year, Month, 1)
if dayOfWeek == 0:
firstColumn = 'B'
rangeStart = 7
rangeNext = 0
elif dayOfWeek == 1:
firstColumn = 'C'
rangeStart = 6
rangeNext = 1
elif dayOfWeek == 2:
firstColumn = 'D'
rangeStart = 5
rangeNext = 2
elif dayOfWeek == 3:
firstColumn = 'E'
rangeStart = 4
rangeNext = 3
elif dayOfWeek == 4:
firstColumn = 'F'
rangeStart = 3
rangeNext = 4
elif dayOfWeek == 5:
firstColumn = 'G'
rangeStart = 2
rangeNext = 5
elif dayOfWeek == 6:
firstColumn = 'H'
rangeStart = 1
rangeNext = 6
for i in xrange(rangeStart):
days = i
days += 1
dt = datetime.datetime(year=Year, month=Month, day=days)
dayInt = calendar.weekday(Year, Month, days)
dayName = calendar.day_name[dayInt]
coloumnstr = str(chr(ord(firstColumn) + i))
worksheet.write_string(coloumnstr+'1' , dayName, cell_format)
worksheet.write_datetime(coloumnstr+'2' , dt, date_format)
for i in xrange(8-rangeNext):
days = i
for x in xrange(7):
days += 1
coloumnCounter = x
coloumnCounter += 1
dt = datetime.datetime(year=Year, month=Month, day=days)
dayInt = calendar.weekday(Year, Month, days)
dayName = calendar.day_name[dayInt]
coloumn = ['B', 'C', 'D', 'E', 'F', 'G', 'H']
coloumnstr = str(coloumn[x])
worksheet.write_string(coloumnstr+'5' , dayName, cell_format)
worksheet.write_datetime(coloumnstr+'6' , dt, date_format)
for i in xrange(15-rangeNext):
days = i
for x in xrange(7):
days += 1
dt = datetime.datetime(year=Year, month=Month, day=days)
dayInt = calendar.weekday(Year, Month, days)
dayName = calendar.day_name[dayInt]
coloumn = ['B', 'C', 'D', 'E', 'F', 'G', 'H']
coloumnstr = str(coloumn[x])
worksheet.write_string(coloumnstr+'10' , dayName, cell_format)
worksheet.write_datetime(coloumnstr+'11' , dt, date_format)
getMonthDays = calendar.monthrange(Year,startMonth)
for i in xrange(22-rangeNext):
days = i
for x in xrange(7):
days += 1
dt = datetime.datetime(year=Year, month=Month, day=days)
dayInt = calendar.weekday(Year, Month, days)
dayName = calendar.day_name[dayInt]
coloumn = ['B', 'C', 'D', 'E', 'F', 'G', 'H']
coloumnstr = str(coloumn[x])
worksheet.write_string(coloumnstr+'15' , dayName, cell_format)
worksheet.write_datetime(coloumnstr+'16' , dt, date_format)
for i in xrange(29-rangeNext):
days = i
for x in xrange(7):
days += 1
dt = datetime.datetime(year=Year, month=Month, day=days)
dayInt = calendar.weekday(Year, Month, days)
dayName = calendar.day_name[dayInt]
coloumn = ['B', 'C', 'D', 'E', 'F', 'G', 'H']
coloumnstr = str(coloumn[x])
worksheet.write_string(coloumnstr+'20' , dayName, cell_format)
worksheet.write_datetime(coloumnstr+'21' , dt, date_format)
workbook.close()
继续并确保安装了http://xlsxwriter.readthedocs.io/并运行脚本。
2018,2019,2020 =给我错误
2038 =完美地运作
2040 =工作,但没有给我所有日期
答案 0 :(得分:1)
我用过这个:python combine 'while loop' with 'for loop' to iterate through some data
让它像这样工作:
while (keep_running):
for i in range(len(totalDaysList)):
if not totalDaysList[i] < totalMonthDays:
# this will effectively
# stop the while loop:
keep_running = False
break
elif totalDaysList[i] < rangeStart:
days += 1
dt = datetime.datetime(year=Year, month=Month, day=days)
dayInt = calendar.weekday(Year, Month, days)
dayName = calendar.day_name[dayInt]
coloumnstr = str(chr(ord(firstColumn) + i))
worksheet.write_string(coloumnstr+'1' , dayName, cell_format)
worksheet.write_datetime(coloumnstr+'2' , dt, date_format)
elif totalDaysList[i] < rangeStart+7:
count1 += 1
days += 1
dt = datetime.datetime(year=Year, month=Month, day=days)
dayInt = calendar.weekday(Year, Month, days)
dayName = calendar.day_name[dayInt]
coloumn = ['B', 'C', 'D', 'E', 'F', 'G', 'H']
coloumnstr = str(coloumn[count1])
worksheet.write_string(coloumnstr+'5' , dayName, cell_format)
worksheet.write_datetime(coloumnstr+'6' , dt, date_format)
elif totalDaysList[i] < rangeStart+14:
........... ......
谢谢!