Here its printing sunday in all the cells in the table
docu = docx.Document()
daylist = ["monday", "tuesday", "wednesday",
"thursday", "friday", "saturday", "sunday"]
num_of_days = int(input('enter days'))
num_of_lectures = int(input('lecture number'))
timetable = docu.add_table(rows=num_of_days, cols=num_of_lectures)
tablecell = timetable.cell(0, 1)
tablerow = timetable.rows[1]
for day in daylist:
for tablerow in timetable.rows:
for tablecell in tablerow.cells:
tablecell.text = day
docu.save('timetable.docx')
and if I'm doing this it's printing the whole list in each cell.
docu = docx.Document()
daylist = ["monday", "tuesday", "wednesday",
"thursday", "friday", "saturday", "sunday"]
num_of_days = int(input('enter days'))
num_of_lectures = int(input('lecture number'))
timetable = docu.add_table(rows=num_of_days, cols=num_of_lectures)
tablecell = timetable.cell(0, 1)
tablerow = timetable.rows[1]
for tablerow in timetable.rows:
for tablecell in tablerow.cells:
tablecell.text = (day for day in daylist)
docu.save('timetable.docx')
and I've tried
daylist = ["monday", "tuesday", "wednesday",
"thursday", "friday", "saturday", "sunday"]
num_of_days = int(input('enter days'))
num_of_lectures = int(input('lecture number'))
timetable = docu.add_table(rows=num_of_days, cols=num_of_lectures)
tablerow = timetable.rows[1]
i = 0
for cells in timetable.rows:
i += 1
tablecell = timetable.cell(i, 0)
cells.text = (day for day in daylist)
docu.save('timetables.docx')
I need one string from daylist list in only top row. Python docx documentation is difficult to understand.
(day for day in daylist)will return?daylistone by one?(day for day in daylist)is a generator expression, which yields values fromdaylist. I think I focused on the wrong thing, I should have asked whattablecell.text = (day for day in daylist)does.tablecellwill be the first cell in the table and it keeps iterating in the first row because oftimetable.rows[1](which is wrong) and with that iterating with strings from daylist at the same time. I thought I'd get the desired output.