【问题标题】:Openpyxl: We found a problem with some contentOpenpyxl:我们发现一些内容有问题
【发布时间】:2020-08-26 08:33:41
【问题描述】:

打开我使用 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)

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

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

提前非常感谢!

【问题讨论】:

  • 您需要先缩小问题范围再提问。

标签: python excel openpyxl


【解决方案1】:

我也遇到了同样的问题,这是因为公式写错了。我在打开文件时单击“查看”而不是“删除”发现了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2020-06-28
    • 2020-12-06
    • 2021-04-13
    • 2019-12-08
    • 2016-10-16
    • 1970-01-01
    相关资源
    最近更新 更多