how to handle equals sign ?

91 views
Skip to first unread message

Reinis Reiņikovs

unread,
Jan 3, 2025, 7:18:59 AMJan 3
to openpyxl-users
Hello,

let's say I want to write == to cell

import openpyxl
import sys
wb = openpyxl.Workbook()
ws = wb.active

ws['A1'] = "=="
ws['B1'] = "'=="

wb.save("sample.xlsx")
print (f'{sys.version}')
print(f'{openpyxl.__version__=}')


3.11.9 (tags/v3.11.9:de54cf5, Apr  2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]
openpyxl.__version__='3.1.5'


Opening excel give error and clears A1

B1 is displayed as '== 
I was expecting ==, when I enter B1 and press enter B1 is displayed as ==

General question is how to print arbitrary  data as string (it might start with =) to excel and not expect error.

Regards,
Reinis



Charlie Clark

unread,
Jan 16, 2025, 6:11:27 AMJan 16
to openpyxl-users
On 3 Jan 2025, at 13:18, Reinis Reiņikovs wrote:

> General question is how to print arbitrary data as string (it might start
> with =) to excel and not expect error.

You're actually asking a question that we can't answer because it's related to Excel's behaviour, which is, in many cases, undefined. Openpyxl does follow the convention that a string that starts with an "=" is a formula, and I think it even attempts to validate this now. Otherwise, prepending with a single quote is probably the best approach.

Charlie

--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Sengelsweg 34
Düsseldorf
D- 40489
Tel: +49-203-3925-0390
Mobile: +49-178-782-6226
Reply all
Reply to author
Forward
0 new messages