Home > OS >  How to add an Excel formula containing commas and quotes to a CSV file?
How to add an Excel formula containing commas and quotes to a CSV file?

Time:01-11

I'm trying to output a CSV file from Python and make one of the entries a Google sheet formula:

This is what the formula var would look like:

    strLink = "https://xxxxxxx.xxxxxx.com/Interact/Pages/Content/Document.aspx?id="   strId   "&SearchId=0&utm_source=interact&utm_medium=general_search&utm_term=*"
    strLinkCellFormula = "=HYPERLINK(\""   strLink   "\", \""   strTitle   "\")"

and then for each row of the CSV I have this:

    strCSV = strCSV   strId   ", "   "\""   strTitle   "\", "   strAuthor   ", "   strDate   ", "   strStatus   ", "   "\""   strSection   "\", \""    strLinkCellFormula  "\"\n"

Which doesn't quite work, the hyperlink formula for Google sheets is like so:

=HYPERLINK(url, title)

and I can't seem to get that comma escaped. So in my Sheet I am getting an additional column with the title in it and obviously the formula does not work. Any help would be appreciated.

CodePudding user response:

Try using ; as the formula argument separator. It should work the same.

CodePudding user response:

Instead of reinventing the wheel, you should write your CSV rows using the builtin csv.writer class. This takes care of escaping any commas in the data, so you don't need to build your own escape logic. This helps you avoid the mess of escaping in your strLinkCellFormula = ... and strCSV = strCSV ... lines.

For example:

import csv

urls = ["https://google.com", "https://stackoverflow.com/", "https://www.python.org/"]

titles = ["Google", "Stack Overflow", "Python"]

with open("file.csv", "w") as fw:
    writer = csv.writer(fw)
    writer.writerow(["Company", "Website"])
    for u, t in zip(urls, titles):
        formula = f'=HYPERLINK("{u}", "Visit {t}")'
        row = [t, formula]
        writer.writerow(row)

Note that in the line formula = ... above, I used the f-string syntax to format the URL and title into the string. I also used apostrophes to define the string, since I knew that the string was going to contain quotation marks and I didn't want to bother escaping them.

This gives the following CSV:

Company,Website
Google,"=HYPERLINK(""https://google.com"", ""Visit Google"")"
Stack Overflow,"=HYPERLINK(""https://stackoverflow.com/"", ""Visit Stack Overflow"")"
Python,"=HYPERLINK(""https://www.python.org/"", ""Visit Python"")"

where the escaping of commas and quotes is already taken care of. It is also read by Excel/GSheets correctly, since it conforms to the standard CSV format: Excel screenshot

Sheets screenshot


For your specific case, you'd write to your CSV file like so:

with open(filename, "w") as wf:
    writer = csv.writer(wf)
    writer.writerow(headers) # if necessary
    for ...:
        strLink = f"https://xxxxxxx.xxxxxx.com/Interact/Pages/Content/Document.aspx?id={strID}&SearchId=0&utm_source=interact&utm_medium=general_search&utm_term=*"
        strLinkCellFormula = f'=HYPERLINK("{strLink}", "{strTitle}")'
        row = [strId, strTitle, strAuthor, strDate, strStatus, strSection, strLinkCellFormula]
        writer.writerow(row)
  • Related