简体   繁体   English

使用 openpyxl python 在 excel 中插入带空白的公式

[英]Inserting formula with Blank in excel using openpyxl python

I am trying to insert a formula in excel sheet using openpyxl package.我正在尝试使用 openpyxl package 在 excel 表中插入一个公式。 The code is as shown below.代码如下所示。

wb = openpyxl.load_workbook('Review.xlsx')
ws = wb.get_sheet_by_name('Final')
for i, cellObj in enumerate(ws['J'],1):
        if i != 1:
            cellObj.value = "=IFERROR(VLOOKUP(F{0},'mapping'!A:B,2,0),"")".format(i)

for i, cellObj in enumerate(ws['K'],1):
        if i != 1:
            cellObj.value = "=J{0}".format(i)

for i, cellObj in enumerate(ws['L'],1):
        if i != 1:
            cellObj.value = "=IFERROR(VLOOKUP(K{0},'mapping_countries'!B:C,2,0),"")".format(i)

wb.save('Review.xlsx')
wb.close()

In formulas, columns J & L, I am intending to place a blank cell when there is an error.在公式、J 和 L 列中,我打算在出现错误时放置一个空白单元格。 But this is not working and instead i am getting 0 in the cell.但这不起作用,而是我在单元格中得到 0。 Could anyone please help achieve this?任何人都可以帮助实现这一目标吗? Thanks a lot!非常感谢!

I think it has to do with " . Try to escape them \" in these lines:我认为这与" . Try to escape them \"在这些行中有关:

cellObj.value = "=IFERROR(VLOOKUP(F{0},'mapping'!A:B,2,0),\"\")".format(i)
cellObj.value = "=IFERROR(VLOOKUP(K{0},'mapping_countries'!B:C,2,0),\"\")".format(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM