Search code examples
pythonarraysweb-scrapingopenpyxlxlsx

ValueError when trying to write data to xlsx sheet using Openpyxl


I'm trying to save the outputted social media link to an excel file using openpyxl but getting the following error:

Traceback (most recent call last):
  File "/Users/xxxx/_Main_.py", line 40, in <module>
    sheet.cell(cell.row, col2).value = ig_get_present
  File "/Users/xxxx/venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 215, in value
    self._bind_value(value)
  File "/Users/xxxx/venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 184, in _bind_value
    raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert ['https://www.instagram.com/xxxx/'] to Excel

This is the code leading to it, no idea why its happening.

column_name = 'URL'
column_name2 = 'Instagram'

headers = [cell.value for cell in sheet[1]]

col = get_column_letter(headers.index(column_name) + 1)

col2 = headers.index(column_name2) + 1

for cell in sheet[col][1:]:
    url = cell.value
    r = requests.get(url)
    ig_get = ['instagram.com']
    ig_get_present = []
    soup = BeautifulSoup(r.content, 'html5lib')
    all_links = soup.find_all('a', href=True)
    for ig_get in ig_get:
        for link in all_links:
            if ig_get in link.attrs['href']:
                ig_get_present.append(link.attrs['href'])
    sheet.cell(cell.row, col2).value = ig_get_present

Solution

  • Converting the data to a string fixed my issue.

    ig_got = str(ig_get_present)
    sheet.cell(cell.row, col2).value = ig_got