0

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.

7
  • I think I may see the problem now. What do you think (day for day in daylist) will return? Commented Jan 7, 2020 at 21:41
  • string from daylist one by one? Commented Jan 7, 2020 at 21:50
  • (day for day in daylist) is a generator expression, which yields values from daylist. I think I focused on the wrong thing, I should have asked what tablecell.text = (day for day in daylist) does. Commented Jan 7, 2020 at 21:53
  • I assumed tablecell will be the first cell in the table and it keeps iterating in the first row because of timetable.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. Commented Jan 7, 2020 at 22:14
  • Do you understand how it works now? Commented Jan 7, 2020 at 22:20

2 Answers 2

2

The Python code is faithfully doing what you are asking it to do. Perhaps you are confused about how iteration works.

I'm assuming you want a result that looks like this:

+--------+---------+-----------+
| Monday | Tuesday | Wednesday |
+--------+---------+-----------+
|        |         |           |
+--------+---------+-----------+
|        |         |           |
+--------+---------+-----------+

This can be accomplished a variety of ways, but something like this would do the trick:

cells = table.rows[0].cells
for i in num_of_days:
   cells[i].text = daylist[i]

or perhaps better:

for i, cell in enumerate(table.rows[0].cells):
    cell.text = daylist[i]

slightly fancier would be:

for cell, title in zip(table.rows[0].cells, daylist):
    cell.text = title
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I'm still trying to understand how concepts in docx work. I'm not able to understand meaning of few methods and how they link together.
Keep working at it, it will come to you sooner than you think! :)
0

import itertools

tablerow = timetable.rows[0]

for (day, cell) in zip(itertools.cycle(daylist), tablerow):
    cell.text = day

1 Comment

add context to prevent down-voting and/or bot removal of answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.