Search code examples
pythonexcelpandasdataframexlwings

xlwings removes ' from my string stored in a dataframe object when I try to paste it into excel


I am trying to paste a dataframe into excel using xlwings.

one of the columns holds the name of the item and some of those names starts with ' eg. 'FIRST' itemname

When I use

xw.Range(startcell, index=False, header=False).value = df

xlwings removes the ' at the beginning so 'FIRST' itemname becomes FIRST' itemname.

I have printed out the dataframe before trying to paste it in with xlwings and there everything looks as it should and it stores the name as an object type when I call df.dtypes

EDIT: on a closer inspection it does carry over to Excel, however, even though it is written correctly in the formula bar it isn't in the actual cell.

How can I make the ' carry over and get pasted into excel along with everything else?


Solution

  • I'll answer my own question.

    Someone informed me Excel will always treat a leading ' as text so if I want to actually have it write ' to begin with I have to make it a double ' so ''FIRST' itemname. I'll handle that in the dataframe before pasting it