8

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)

Nicholas
  • 3,517
  • 13
  • 47
  • 86

2 Answers2

16

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)
5

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

turtle_in_mind
  • 986
  • 1
  • 18
  • 36