Openpyxl:我们发现一些内容有问题

Mar*_* F. 5 python excel openpyxl

我在打开我用 openpyxl 生成的文件时收到错误消息“我们发现某些内容有问题”。该文件是通过连接不同的 xlsx 文件并在其他单元格中添加其他公式来生成的。

问题是由我正在写入单元格的带有 if 条件的公式引起的(第二个 for 循环导致了 excel 错误消息)。

那是代码:

import openpyxl as op
import glob

# Search for all xlsx files in directory and assign them to variable allfiles
allfiles = glob.glob('*.xlsx')
print('Following files are going to be included into the inventory: ' + str(allfiles))

# Create a workbook with a sheet called 'Input'
risk_inventory = op.load_workbook('./Report/Risikoinventar.xlsx', data_only = False)
input_sheet = risk_inventory['Input']
risk_inventory.remove(input_sheet)
input_sheet = risk_inventory.create_sheet()
input_sheet.title = 'Input'
r_maxrow = input_sheet.max_row + 1

# There is more code here which is not related to the problem

for i in range (2,r_maxrow):
    if input_sheet.cell(row = i, column = 2).value == 'Top-Down':
        input_sheet.cell(row = i, column = 20).value = '=IF(ISTEXT(H{}),0,IF(H{}<=1000000,1,IF(H{}<=2000000,2,IF(H{}<=4000000,3,IF(H{}<=8000000,4,IF(H{}>8000000,5,0))))))'.format(i,i,i,i,i,i)
    elif input_sheet.cell(row = i, column = 2).value == 'Bottom-Up':
        input_sheet.cell(row = i, column = 20).value = '=IF(ISTEXT(H{}),0,IF(H{}<=1000000,1,IF(H{}<=2000000,2,IF(H{}<=4000000,3,IF(H{}<=8000000,4,IF(H{}>8000000,5,0))))))'.format(i,i,i,i,i,i)

for i in range (2,r_maxrow):
    if input_sheet.cell(row = i, column = 2).value == 'Top-Down':
        input_sheet.cell(row = i, column = 21).value = '=IF(K{}="Sehr gering",1,IF(K{}="Gering",2,IF(K{}="Mittel",3,IF(K{}="Hoc",3,IF(K{}="Sehr hoch",3,0))))))'.format(i,i,i,i,i,i)
    elif input_sheet.cell(row = i, column = 2).value == 'Bottom-Up':
        input_sheet.cell(row = i, column = 21).value = '=IF(K{}="Sehr gering",1,IF(K{}="Gering",2,IF(K{}="Mittel",3,IF(K{}="Hoc",3,IF(K{}="Sehr hoch",3,0))))))'.format(i,i,i,i,i,i)
Run Code Online (Sandbox Code Playgroud)

因此,根据单元格中的信息(行 = i,列 = 2)我想要单元格中的特定公式(行 = i,列 = 21)。第一个 for 循环完美运行,第二个 for 循环导致 excel 中的错误消息并且没有粘贴公式)

正如您可能已经看到的那样,我尝试用 Python 编码一个星期,但以前从未尝试过编码……

提前谢谢了!

poi*_*occ 1

我也遇到了同样的问题,这是由于公式写得不正确造成的。我在打开文件时单击“查看”而不是“删除”发现了问题所在。