0

My target is that I open an excel file, add a new column and save it as a new file. Unfortunately the code crashes at the save position.

import pandas as pd

path = '//My Documents/Python/'
fileName = "test.xlsx"

ef = pd.ExcelFile(path+fileName)

df = pd.read_excel(path+fileName, sheet_name=ef.sheet_names[0])

i = 1
for test in df['Content']:
    try:
        df['Content'] = df['Content'].astype(str)
    except:
        print("An exception occurred")
        break
    i += 1

print('success')
df.to_excel('/My Documents/Python/test_NEW.xlsx')

The error message

with link or location/anchor > 255 characters since it exceeds Excel's limit for URLS force_unicode(url)) Traceback (most recent call last):

Now my question is, is the save method wrong? How can I save the the columns with more than 256 characters or how can I cut the link after 256 characters ?

Thank you in advance! I would be very happy to receive an answer.

Tomaschitz
  • 31
  • 3
  • what is the use of `for test in df['Content']` in your code? – Shijith Dec 17 '19 at 07:34
  • refer [here](https://stackoverflow.com/questions/35440528/how-to-save-in-xlsx-long-url-in-cell-using-pandas) to save url as text – Shijith Dec 17 '19 at 07:35
  • Thank you. I changed it to `ef = pd.ExcelWriter(path+fileName, options={'strings_to_urls': False})` . Unfortunately I'm working with `df` . So I changed every `df` to `ef` but now `ef['Content']` is wrong and I think the others are also false – Tomaschitz Dec 17 '19 at 07:59
  • if `ef = pd.ExcelWriter(path+fileName, engine='xlsxwriter',options={'strings_to_urls': False})`, then do `df.to_excel(ef)`. you need to have `xlsxwriter` library installed – Shijith Dec 17 '19 at 08:40
  • Thank you. But the problem is here `for test in df['Content']:` I changed `df` to `ef` but `ef` doesn't know `ef['Content']:` – Tomaschitz Dec 17 '19 at 09:52
  • `ef` is an `ExcelWriter` object not a dataframe – Shijith Dec 17 '19 at 09:54
  • I know. How can I change my code that the code correct is ? :) – Tomaschitz Dec 17 '19 at 09:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204400/discussion-between-shijith-and-tomaschitz). – Shijith Dec 17 '19 at 09:57

0 Answers0