python公式从另一个表获取值与python有空间

时间:2014-12-17 15:50:45

标签: python excel xlwt

Python不能接受带空格的公式。

像这样的'原始数据'表

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

它会显示错误

如果我使用这个

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

它也显示错误

如果我使用这个

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

它也是错误

有没有办法运行公式?我会尝试任何建议。感谢

2 个答案:

答案 0 :(得分:1)

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

错误

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

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

错误 enter image description here

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

错误

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

答案 1 :(得分:1)

你需要转义字符串中的引号,或者在外层使用三引号,如下所示:

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

请参阅有关strings, quotes and escaping的Python文档。