How to save in *.xlsx long URL in cell using Pandas

前端 未结 2 523
醉梦人生
醉梦人生 2020-12-24 12:56

For example I read excel file into DataFrame with 2 columns(id and URL). URLs in input file are like text(without hyperlinks):

input_f = pd.read_excel(\"inpu         


        
相关标签:
2条回答
  • 2020-12-24 13:16

    You can create an ExcelWriter object with the option not to convert strings to urls:

    writer = pandas.ExcelWriter(r'file.xlsx', engine='xlsxwriter',options={'strings_to_urls': False})
    df.to_excel(writer)
    writer.close()
    
    0 讨论(0)
  • 2020-12-24 13:24

    I tried it myself and got the same problem. You could try to create a temp csv file and then use xlsxwriter to create an excel file. Once done then delete the tmp file. xlsxwriter has a write_string method that will override the auto hyperlinking that excel does. This worked for me.

    import pandas as pd
    import csv
    import os
    from xlsxwriter.workbook import Workbook
    inData = "C:/Users/martbar/Desktop/test.xlsx"
    tmp = "C:/Users/martbar/Desktop/tmp.csv"
    exFile = "C:/Users/martbar/Desktop/output.xlsx"
    
    #read in data
    df = pd.read_excel(inData)
    
    #send to csv
    df.to_csv(tmp, index=False)
    
    #convert to excel
    workbook = Workbook(exFile)
    worksheet = workbook.add_worksheet()
    with open(tmp, 'r') as f:
        reader = csv.reader(f)
        for r, row in enumerate(reader):
            for c, col in enumerate(row):
                #if you use write instead of write_string you will get the error
                worksheet.write_string(r, c, col) 
    workbook.close()
    
    #delete tmp file
    os.remove(tmp)
    
    0 讨论(0)
提交回复
热议问题