Home »
Python »
Python Programs
How to save in *.xlsx long URL in cell using Pandas?
Learn, how to save in *.xlsx long URL in cell using Python Pandas?
By Pranit Sharma Last updated : October 06, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Problem statement
Suppose we are given the Pandas dataframe with 2 columns ID and URL. The URL column is a string-type column that contains long hyperlinks.
Saving in *.xlsx long URL in cell using Pandas
The problem is that when we save this data in an excel file, the URL column values are converted into clickable hyperlinks but we do not want that, instead, we want them to be non-clickable in the form of simple strings.
We need to find a way to successfully save these long strings in the cell of xlsx files without being converted into clickable hyperlinks.
For this purpose, we can use the ExcelWriter method to create an ExcelWriter object with the option not to convert string_to_urls.
Let us understand with the help of an example,
Python program to save in *.xlsx long URL in cell using Pandas
# Importing pandas
import pandas as pd
# Importing workbook from xlsxwriter
from xlsxwriter import workbook
# Import numpy
import numpy as np
# Creating a dictionary
d = {
'ID':[90,83,78,76],
'URL':[
'https://www.includehelp.com/python/pandas-text-matching-like-sqls-like.aspx',
'https://www.includehelp.com/python/how-to-get-a-single-value-as-a-string-from-pandas-dataframe.aspx',
'https://www.includehelp.com/python/pandas-pd-series-isin-performance-with-set-versus-array.aspx',
'https://www.includehelp.com/python/pandas-text-matching-like-sqls-like.aspx'
]
}
# Creating a dataframe
df = pd.DataFrame(d)
# Display original DataFrame
print("Original DataFrame:\n",df,"\n")
# Saving this df to using ExcelWriter
writer = pd.ExcelWriter(r'NewFile.xlsx',options={'strings_to_urls': False})
df.to_excel(writer)
writer.close()
# Display a msg
print("File Saved:\n","\n")
Output
The output of the above program is:
Python Pandas Programs »