Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(PYTHON) to_excel - Ignoring URL... 255 characters since it exceeds Excel's limit for URLS

Tags:

python

pandas

I am using the following code to copy a dataframe into an Excel document:

dfPol.to_excel(writer, 'Influence on Policy', columns=colsPol)

The problem is that I am getting the following error as the URLs are too long:

Ignoring URL .... 255 characters since it exceeds Excel's limit for URLS

So, I found a solution which is to use:

dfPol.to_excel(writer, 'Influence on Policy', columns=colsPol, options={'strings_to_urls': False})

But I am getting an error of:

TypeError: to_excel() got an unexpected keyword argument 'options'

As obviously options does not work with to_excel, which is what I am using for a lot of other code in my script (i.e. I want to stick with 'to_excel')

Are there any solutions to my problem?

This is not a duplicate of: How to save in *.xlsx long URL in cell using Pandas as that is not about to_excel (it is about excelwriter)

like image 801
ScoutEU Avatar asked Oct 15 '17 14:10

ScoutEU


2 Answers

Pass the argument options={'strings_to_urls': False} to the pd.ExcelWriter call when creating your writer. It will look something like this:

writer = pd.ExcelWriter('C:/Users/{}/Desktop/RF Data.xlsx'.format(staName2),options={'strings_to_urls': False})
dfPol.to_excel(writer, 'Influence on Policy', columns=colsPol)
like image 61
Mykola Shchetinin Avatar answered Oct 08 '22 17:10

Mykola Shchetinin


you can just use:

 with pd.ExcelWriter(file_name, options={'strings_to_urls': False}) as writer:
        df.to_excel(writer, 'Influence on Policy', columns=colsPol)

Then you don't have to worry about closing your writer file

like image 5
turtle_in_mind Avatar answered Oct 08 '22 16:10

turtle_in_mind