Iterating over 2 or more columns

1,111 views
Skip to first unread message

Kyle Felipe Vieira Roberto

unread,
Jul 18, 2017, 3:34:21 PM7/18/17
to openpyxl-users
hi people!
I'm new using python and openpyxl, and i was trying to iterate over some columns and i had some trouble.


i was trying to make an dictionary with some data from a sheet.

the collumn CNPJ (column B) and column EMPRESA

This is the code:

cnpj_nome = {} Dictionary
for coluna_1, coluna_2 in folhas.iter_rows('B{}:B{}','C{}:C{}' .format(folhas.min_row + 1, folhas.max_row,
folhas.min_row + 1, folhas.max_row)):
for celula_1, celula_2 in coluna_1, coluna_2:
cnpj_nome[celula_1.text] = celula_2.text
print(cnpj_nome)


and, this is the error:

"/home/kylefelipe/Dropbox/notifica_tax/notificar_tax/lib/python3.6/site-packages/openpyxl/worksheet/worksheet.py:495: UserWarning: Using a range string is deprecated. Use ws[range_string]
warn("Using a range string is deprecated. Use ws[range_string]")
Traceback (most recent call last):
File "/home/kylefelipe/Dropbox/projetos pycharm/Word/manipular_planilha.py", line 17, in 
folhas.min_row + 1, folhas.max_row)):
File "/home/kylefelipe/Dropbox/notifica_tax/notificar_tax/lib/python3.6/site-packages/openpyxl/worksheet/worksheet.py", line 496, in iter_rows
min_col, min_row, max_col, max_row = range_boundaries(range_string.upper())
File "/home/kylefelipe/Dropbox/notifica_tax/notificar_tax/lib/python3.6/site-packages/openpyxl/utils/cell.py", line 129, in range_boundaries
raise ValueError("{0} is not a valid coordinate or range")
ValueError: {0} is not a valid coordinate or range'

Charlie Clark

unread,
Jul 19, 2017, 3:52:02 AM7/19/17
to openpyx...@googlegroups.com
Am .07.2017, 21:34 Uhr, schrieb Kyle Felipe Vieira Roberto
<kylef...@gmail.com>:

> ValueError: {0} is not a valid coordinate or range'

The error message really is self-explanatory: you're passing in an invalid
range.

You're actually making life much more difficult for yourself. Please
refer to documentation:
http://openpyxl.readthedocs.io/en/latest/tutorial.html#accessing-many-cells
on how to use iter_rows() or iter_cols()

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

Kyle Felipe Vieira Roberto

unread,
Jul 19, 2017, 9:26:54 AM7/19/17
to openpyxl-users
Thx man.... the code was wrong, i fixed it yesterday...
this is the script working as well...

Kyle Felipe Vieira Roberto

unread,
Jul 21, 2017, 5:43:31 PM7/21/17
to openpyxl-users
Reply all
Reply to author
Forward
0 new messages