Am .03.2016, 21:55 Uhr, schrieb <
sander...@hotmail.com>:
Yes I agree. I just thought I'd mention it, since the issue arose only
> after having edited the file in LibreOffice. Anyway I'm glad the
> formatting was preserved under Excel. That's what the end users will be
> using.
Then that's what you should use in your toolchain.
> About the advice on the loop, could you please help me understand why my
> earlier version (below) was inferior? If the ranges have to be open to
> change based on different input, would you advise a different method from
> the one you suggested?
>
>
> for i in range(0,9):
> wb2['Sheet'].cell(row=38,column=x).value =
> wb1['Sheet'].cell(row=13,column=y).value
> x+=1
> y+=1
1) legibility: you have a two worksheet and cell look ups in a single
statement. The following is easier to read (and faster):
value = ws1.cell(row=13,column=y).value
ws2.cell(row=38, column=x, value=value)
But it's even nicer without individual cell lookups.
2) maintainability: you're relying manual counters rather than the ranges
themselves. This is very brittle, likely to break, be a PITA to test and
maintain, and slow.
In your earlier code it looked like you had some code that searched.
Nested zip() isn't particularly beautiful – you could also work with some
kind of offsets – but it takes the range definitions out of the loop.
x1 = 13
x2 = x1 + 25
for row in ws1.iter_rows(…):
for c1 in row:
ws2.cell(row=x2, col=c1.col, value=c1.value)
2.4 will make programmatic and ad hoc access of rows and columns easier:
r1 = ws1[13]
r2 = ws2[38]
for c1, c2 in zip(r1, r2):
c2.value = c1.value
What's not to like?