Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop-help for a newbie?
#10
I am getting an error with only the f-string.
Can I get your thoughts?
Error line:
sheet.merge_cells(f'{pair[0]}:{pair[1]}{i}')
Traceback Error:
Traceback (most recent call last):
  File "C:\Python36x64\lib\site-packages\openpyxl\descriptors\base.py", line 57, in _convert
    value = expected_type(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Python36x64/test.py", line 40, in <module>
    sheet.merge_cells(f'{pair[0]}:{pair[1]}{i}')
  File "C:\Python36x64\lib\site-packages\openpyxl\worksheet\worksheet.py", line 705, in merge_cells
    max_col=end_column, max_row=end_row)
  File "C:\Python36x64\lib\site-packages\openpyxl\worksheet\cell_range.py", line 58, in __init__
    self.min_row = min_row
  File "C:\Python36x64\lib\site-packages\openpyxl\descriptors\base.py", line 107, in __set__
    value = _convert(self.expected_type, value)
  File "C:\Python36x64\lib\site-packages\openpyxl\descriptors\base.py", line 59, in _convert
    raise TypeError('expected ' + str(expected_type))
TypeError: expected <class 'int'>
Thanks,
Phil

Reading the Traceback, I see it looks like a type error due to Openpyxl.
What's the best way to troubleshoot this? Looking in the Openpyxl docs under the base.py, cell_range.py, and/or worksheet.py modules for '_convert' type errors?
This error says,
TypeError: int() argument must be a string
and
TypeError: expected <class 'int'>
Confusing!

f-strings are new in 3.7(?) version.
How could I do this loop without an f-string, very simple so a newbie could understand the loop easily?

Thanks,
phil

looking over the base.py source code, I can see the cause of the error. I'm unsure how to fix it with the f-string.

def _convert(expected_type, value):
    """
    Check value is of or can be converted to expected type.
    """
    if not isinstance(value, expected_type):
        try:
            value = expected_type(value)
        except:
            raise TypeError('expected ' + str(expected_type))
    return value
Source code for openpyxl.descriptors.base

If I understand this, it's looking for a string with the _convert() function which is held within the __set__() function which is called a tthe bottom of the base.py file:
super(MatchPattern, self).__set__(instance, value)
I've added extra quotes around the f-string, but that failed too.
How to continue logical troubleshooting with this code error?
Thanks,
I'm trying Wall
Phil

Okay, I don't know if anyone is still with me, but I'll continue to document my step-by-step for anyone else behind me.
I've looked over the Openpyxl source code for
  1. base.py
  2. cell_range.py
  3. worksheet.py
In the worksheet module, lines 703-725 it talks about merging.
Specifically, look at the comment on line 706:
def merge_cells(self, range_string=None, start_row=None, start_column=None, end_row=None, end_column=None):
        cr = CellRange(range_string=range_string, min_col=start_column, min_row=start_row,
                      max_col=end_column, max_row=end_row)
        """ Set merge on a cell range.  Range is a cell range (e.g. A1:E1) """

        self.merged_cells.add(cr.coord)
        self._clean_merge_range(cr)
so I think the f-string code,
sheet.merge_cells(f'{pair[0]}:{pair[1]}{i}')
, is not meeting the type function required by this worksheet module.
How could I adjust my code to loop over my repetitive lines while fulfilling the worksheet module requirements of
Range is a cell range (e.g. A1:E1)
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,163 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