openpyxl error raise ValueError('Min value is {0}'.format(self.min)) in opening heavy file with formatting

Question:

I’m trying to use openpyxl for the first time on a very heavy file, that happens to be over 20 500 Ko, has a lot of formatting and a VBA macro.

My code keeps returning the following error:

File " Anaconda3libsite-packagesopenpyxlstylesalignment.py", line 52, in __init__
    self.relativeIndent = relativeIndent
 
  File " Anaconda3libsite-packagesopenpyxldescriptorsbase.py", line 107, in __set__
    raise ValueError('Min value is {0}'.format(self.min))
 
ValueError: Min value is 0

Would anyone know what the problem is / how to access the file despite it? I’m trying to post data into an existent Excel file to simplify processes and replace a heavy VBA code. So I can’t just post it into a different xlsx file and call it using a VBA code (that would defeat the purpose).

Thanks a lot!

Here is my code :

wb = load_workbook(filename='C:/dev/CodeRep/ProjectName/MainFile 2021_01.xlsm', read_only = False, keep_vba = True)
Asked By: Clara Loizeau

||

Answers:

The traceback says that there is a problem with the Alignment definition in the workbook’s stylesheet. openpyxl follows the OOXML specification very closely to minimise unpleasant surprises later, this is why it tends to raise exceptions or give warnings rather than let things pass.

For more details we’ll need to see the XML source for the stylesheet, or the Alignments part at least. You can find this by unzipping the XLSM file and looking for the styles.xml file. That will give you more information and also allow you to submit a bug report to openpyxl.

Answered By: Charlie Clark

I got the same error and wasn’t able to figure out the exact cause, but noticed when I ran my python script in a different environment it worked without issue.

I realized it may have had something to do with the versions of the openpyxl and xlrd packages I was using so I downgraded them to openpyxl==3.0.4 and xlrd==1.2.0 (previously using openpyxl==3.0.7 and xlrd==2.0.1) and that solved my issue.

Answered By: TrillGates

Preprocess the file

I solved this issue by preprocessing the excel file.

Found that mi problem was at "*/myfile.xlsx/xl/styles.xml" where several xf tags had an attribute indent="-1", and openpyxl only supports non-negative values, raising that exception when a negative value is found.

After some time spent trying to override entire openpyxl hierarchy in order to catch the exception, I decided to process the XLSX.

Here is my code:

def fix_xlsx(file_name):
    with zipfile.ZipFile(file_name) as input_file, zipfile.ZipFile(file_name + ".out", "w") as output_file:
         # Iterate over files
         for inzipinfo in input_file.infolist():
            with input_file.open(inzipinfo) as infile:
                if "xl/styles.xml" in inzipinfo.filename:
                    # Read, Process & Write
                    lines = infile.readlines()
                    new_lines = b"n".join([line.replace(b'indent="-1"', b'indent="0"') for line in lines])
                    output_file.writestr(inzipinfo.filename, new_lines)
                else:
                    # Read & Write
                    output_file.writestr(inzipinfo.filename, b"n".join([line for line in infile.readlines()]))
    # Replace file
    os.replace(file_name + ".out", file_name)

Disclaimer:

I must say this is not a very elegant solution as the entire file is processed, and an auxiliary file is used.
Also I am not so expert at excel to tell wheter changing that indent="-1" to indent="0" for those tags might cause format problems in the file. This is my working solution and can’t really tell the effect of those tags.

Answered By: user123n't

I had the same issue — the file wasn’t accepted by Openpyxl.

I just opened the file in MS Excel and saved it to a new file. And it worked after that.

Answered By: Nitin Nain

I ran into this issue, my solution was to pinpoint what was causing the error in the spreadsheet (had something to do with a table that was recently modified) and reconstruct that table in the worksheet. much easier for me than debugging openpyxl or xml.

Answered By: Sammy Douglas