Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python formula get value from another sheet have space with python

Tags:

python

excel

xlwt

Python can't accept formula with space in it.

Like this the 'original data' sheet

sheet.write_merge(0, 0, 5, 8, xlwt.Formula('IF(Original Data!B4<>"",Original Data!B4,"")'), center)

it will show error

if i use this

sheet.write_merge(0, 0, 5, 8, xlwt.Formula('IF('Original Data'!B4<>"",'Original Data'!B4,"")'), center)

it also show error

if i use this

sheet.write_merge(0, 0, 5, 8, xlwt.Formula("IF('Original Data'!B4<>"",'Original Data'!B4,"")"), center)

it also error

Is there a way to run the formula? I'll try any suggestion. Thanks

like image 437
lalalala Avatar asked Nov 27 '25 17:11

lalalala


2 Answers

You need to either escape the quotes in the string or else use triple quotes at the outer level like this:

sheet.write_merge(0, 0, 5, 8, xlwt.Formula("""IF('Original Data'!B4<>"",'Original Data'!B4,"")"""), center)

See the Python docs about strings, quotes and escaping.

like image 99
jmcnamara Avatar answered Nov 29 '25 07:11

jmcnamara


sheet.write_merge(0, 0, 5, 8, xlwt.Formula('IF(Original Data!B4<>"",Original Data!B4,"")'), center)

error

raise ExcelFormulaParser.FormulaParseException, "can't parse formula " + s
FormulaParseException: can't parse formula IF(Original data!B3<>"";Original data!B3;"")

next

sheet.write_merge(0, 0, 5, 8, xlwt.Formula('IF('Original Data'!B4<>"",'Original Data'!B4,"")'), center)

error enter image description here

next

sheet.write_merge(0, 0, 5, 8, xlwt.Formula("IF('Original Data'!B4<>"",'Original Data'!B4,"")"), center)

error

raise ExcelFormulaParser.FormulaParseException, "can't parse formula " + s
FormulaParseException: can't parse formula IF('Original data'!B3<>;'Original data'!B3;)
like image 37
lalalala Avatar answered Nov 29 '25 08:11

lalalala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!