Convert post.get('href') to text or string form, Excel can not handle hyperlinks over 255 characters

Viewed 119

When I write URLs to Excel (from a Python script), any over 255 characters are not written. But I don't actually need the URL to have a hyperlink attached to it.

For example I need 'www.ebay.co.uk' but I do not need 'www.ebay.co.uk'. As long as I have the text, then the next part of my script will work fine.

I tried str() and .text but had no luck with either

str()

if (post.get('title'))!=None:
   df1.loc[Row] = [str((post.get('href'))),Row]
   Row = Row + 1

.text

if (post.get('title'))!=None:
    df1.loc[Row] = [(post.get('href')).text,Row]
    Row = Row + 1
1 Answers

pandas.ExcelWriter has an options parameter that you can combine with xlsxwriter engine to prevent string-to-URL conversion:

df = pd.DataFrame({
    'data': [
        'http://www.python.org/',
        'http://' + 'a'*250,
    ]
})

with pd.ExcelWriter('0.xlsx',
                    engine='xlsxwriter',
                    options={'strings_to_urls': False}) as writer:
    df.to_excel(writer)

links as strings

Source: XlsxWriter Docs

Related