Home » excel » excel – python formula get value from another sheet have space with python

excel – python formula get value from another sheet have space with python

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

How to&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.

Answer:

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;)