add hyperlink to excel sheet created by pandas dataframe to_excel method

后端 未结 5 1604
栀梦
栀梦 2020-12-15 11:56

I have converted a pandas DataFrame to an Excel sheet using df.to_excel.

Now, I want to add hyperlinks to the values in one column. In other words, when a customer

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-15 12:01

    I was generating text files from a excel file and wanted to link the names of the generated .txt files to a particular existing column in the Dataframe.

    I was trying to push the local drive directory where the generated .txt files are stored, to the corresponding "File Name". So that on clicking the file name, it will open the .txt file.

    rancheck_DF = pd.read_excel(excel_file, delim_whitespace = True, encoding = 'utf-8')
    
    for index_df in range(len(rancheck_DF)):
    
        Desc = rancheck_DF.loc[index_df,'Description Text']
    
        MainFile = rancheck_DF.loc[index_df,'File Name']
    
        fileName = r'.\Documents\TestF\TestF_{}.txt'.format(index_df)
    
        with open(fileName, 'w', encoding='utf-8') as txtfile:
            txtfile.write(Desc)
    
        rancheck_DF.loc[index_df,'File Name'] = '=HYPERLINK("{}","{}")'.format(fileName,MainFile)
    
    rancheck_DF.to_excel('./damn.xlsx', index=None)
    

提交回复
热议问题