Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop-help for a newbie?
#21
Thank you, thanks so much. It's really hard being a new coder, who's not natural at it, but driven.
It seems when one question is answered, two others are not being solved.
I'm getting there Dance

Next question:
Why can't I change the active sheet?
Here's the code:
# create workbook
wb = openpyxl.Workbook()
print('all.sheetnames:', wb.sheetnames)
# set sheets
sheet = wb.active
sheet.title = 'Daily_Rounds'
s0 = sheet
s1 = wb.create_sheet('Colors')
print('all.sheetnames:', wb.sheetnames)
print('wb.index(s0/Daily_Rounds) =', wb.index(s0))
print('wb.index(s1/Colors) =', wb.index(s1))

# help: how do you change the active sheet?
print('sheet/wb.active =', sheet)
wb.active = wb.index(s1)
print('sheet/wb.active =', sheet)
I've tried a number of different combinations, discovered how to index through the active sheet cells, all without changing the sheet from 'Daily Rounds' to 'Colors'.

How do you change the active sheet to be able to write to it?

I owe you beers! Clap
Phil

Can I explain this code out?

start_col = [2, 4, 6, 8]
end_col = [x + 1 for x in start_col]
 
for i in range(1, 6):
        if i in [1, 4]:
            sheet.merge_cells(start_row=i, start_column=1, end_row=i, end_column=9)
        else:
            for col1, col2 in zip(start_col, end_col):
                sheet.merge_cells(start_row=i, start_column=col1, end_row=i, end_column=col2)
  • Create a 'start' list with 2, 4, 6, & 8 integers
  • create an 'end' list that adds +1 for every integer in the 'start' list
  • For every value (named i) within the range 1 to 6, but not including 6:
  • if i is either 1 or 4:
  • merge cells starting with row range from 1 to 5, going from columns 1 to 9
  • or else:
  • for variables col1 & col2 in the zip() function calling start_col & end_col:
  • merge cells within row i (range from 1 to 5), going from col1 to col2
I'm not exactly clear on everything here, specifically the objective of the zip() function and what value the col1 and col2 have.

I see the zip() defination but it's still not clear (must be 02:30am!):
Quote:The zip() function take iterables (zero or more), makes iterator that aggregates elements based on the iterables passed, and returns an iterator of tuples.
Return Value from zip()
The zip() function returns an iterator of tuples based on the iterable object.
If no parameters are passed, zip() returns an empty iterator
If a single iterable is passed, zip() returns an iterator of 1-tuples. Meaning, the number of elements in each tuple is 1.
If multiple iterables are passed, ith tuple contains ith Suppose, two iterables are passed; one iterable containing 3 and other containing 5 elements. Then, the returned iterator has 3 tuples. It's because iterator stops when shortest iterable is exhaused.

Is my description correct? What am I missing?
Thanks,
phil
Reply


Messages In This Thread
Loop-help for a newbie? - by pcsailor - Aug-29-2018, 04:14 AM
RE: Loop-help for a newbie? - by perfringo - Aug-29-2018, 04:59 AM
RE: Loop-help for a newbie? - by pcsailor - Aug-29-2018, 05:09 AM
RE: Loop-help for a newbie? - by perfringo - Aug-29-2018, 05:45 AM
RE: Loop-help for a newbie? - by buran - Aug-29-2018, 05:48 AM
RE: Loop-help for a newbie? - by perfringo - Aug-29-2018, 06:05 AM
RE: Loop-help for a newbie? - by buran - Aug-29-2018, 06:20 AM
RE: Loop-help for a newbie? - by perfringo - Aug-29-2018, 07:26 AM
RE: Loop-help for a newbie? - by pcsailor - Aug-30-2018, 04:04 AM
RE: Loop-help for a newbie? - by pcsailor - Aug-31-2018, 04:09 AM
RE: Loop-help for a newbie? - by buran - Aug-31-2018, 04:11 AM
RE: Loop-help for a newbie? - by pcsailor - Aug-31-2018, 04:44 AM
RE: Loop-help for a newbie? - by buran - Aug-31-2018, 04:54 AM
RE: Loop-help for a newbie? - by pcsailor - Aug-31-2018, 05:17 AM
RE: Loop-help for a newbie? - by pcsailor - Aug-31-2018, 10:03 AM
RE: Loop-help for a newbie? - by buran - Aug-31-2018, 11:32 AM
RE: Loop-help for a newbie? - by pcsailor - Sep-01-2018, 04:32 AM
RE: Loop-help for a newbie? - by buran - Sep-01-2018, 05:18 AM
RE: Loop-help for a newbie? - by perfringo - Sep-01-2018, 05:25 AM
RE: Loop-help for a newbie? - by buran - Sep-01-2018, 05:34 AM
RE: Loop-help for a newbie? - by pcsailor - Sep-01-2018, 07:28 AM
RE: Loop-help for a newbie? - by pcsailor - Sep-02-2018, 02:42 AM
RE: Loop-help for a newbie? - by buran - Sep-02-2018, 05:46 AM
RE: Loop-help for a newbie? - by perfringo - Sep-02-2018, 07:39 AM
RE: Loop-help for a newbie? - by pcsailor - Sep-02-2018, 07:00 AM
RE: Loop-help for a newbie? - by buran - Sep-02-2018, 07:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  newbie while loop error: IndexError: list assignment index out of range msa969 3 75,082 Mar-31-2017, 12:24 PM
Last Post: msa969

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020