Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openpyxl: We found a problem with some content

I am getting the error message 'We found a problem with some content' opening a file I generated with openpyxl. The file is being generated by concatenating different xlsx files and adding additional formulas in further cells.

The problem is caused by a Formula with an if-condition I am writing into a cell (the second for loop is causing the excel error message).

That's the code:

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)

So depending on what information is in cell(row = i, column = 2) I want a specific formula in cell(row = i, column = 21). The first for loop works perfectly, second for loop causes the error message in excel and the formulas are not being pasted in)

As you probably already see I am trying to code with Python for a week an have never ever tried coding before…

Many thanks in advance!

like image 348
Marcel F. Avatar asked Nov 06 '22 08:11

Marcel F.


1 Answers

I've been having the same issue, and it was due to an incorrectly written formula. I found what was wrong by clicking "View" instead of "Delete" when opening the file.

like image 75
chris_aych Avatar answered Nov 14 '22 22:11

chris_aych