Pergunta

I have come across weird behaviour when programmatically highlighting cells in openpyxl which was initially described in this post of mine. I spent some (much more) time investigating this and managed to track the source of this error. I don't know if this is the only way to reproduce it, maybe some steps are not necessary, but the following example code is closest to what I am actually doing:

import openpyxl

def doStuff(iterations=1, fName='test'):
    for i in range(iterations):
        wb = openpyxl.reader.excel.load_workbook(fName+'.xlsx')
        ws=wb.get_active_sheet()
    
        a1=ws.cell('A1')
        a1.style.fill.fill_type=openpyxl.style.Fill.FILL_SOLID
        a1.style.fill.start_color.index='0000CC'
            
        namedRanges=wb.get_named_ranges()
        for namedRange in namedRanges:
             if namedRange.name in 'myName':
                 desiredNamedRange=namedRange
    
        dest=desiredNamedRange.destinations
        namedCell=ws.cell(dest[0][2].split('$')[1]+dest[0][3].split('$')[2])
        namedCell.style.fill.fill_type=openpyxl.style.Fill.FILL_SOLID
        namedCell.style.fill.start_color.index='FF33CC'
    
        wb.save(fName+'.xlsx')

What happens is I open an already-existing workbook with some values (numbers and text). Numbers are located in the cells which I will highlight later (A1 and the one called "myName", which is E7 in my test cases) whereas the text is in adjacent columns. I then fill A1 and the named cell with some colours and save the workbook. This can be repeated a number of times if desired (to simulate prolonged usage of my original code).

After the first run everything works fine as you'd expect. The problem occurs when, after first run, you open the workbook in Excel, apply "no fill" to a range of cells with A1 in it and close the workbook while still keeping those cells selected. When you run my code with 10 or 20 iterations again you'll notice, when you open the test workbook in Excel, that the cell range which you initially left selected is now highlighted to the same colour as A1.

My questions are:

  • Am I doing something wrong here? The nature of the program requires it to do this procedure several times over and I cannot forbid the users from closing their workbooks with cells selected.

  • Is there any way to prevent this from happening? And, if yes, how to do this?

EXTRA INFO I've looked into the XML files which form the workbook where I observed this behaviour (see this website for the standard). I noticed that, for some reasons, the cell style reference changes when you leave them selected when using openpyxl to change the formatting. For example the style of cell B1 (letter s) should be 0 (or non-existent as 0 is assumed by default) whereas openpyxl changed it to 1 when running the above code on the workbook:

<c r="A1" s="1">
    <v>1</v>
</c>
<c r="B1" s="1" t="s">
    <v>0</v>
</c>

This causes the cell style to be changed. I have very strong doubts I am doing something wrong here, so:

My new question is: Which part of the openpyxl module is most likely to be responsible for this? Because something tells me it needs a revision.

Foi útil?

Solução

Well not that I solved this problem (as this would require modifying the library which I have no time for) but I did find a way around this: win32com module in Python and driving Excel from a Python script through the COM. Works like a dream and has vastly more functionality than openpyxl. The only drawback being that you need Excel license in order to use this solution. For anyone interested in doing the same I do recommend this page.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top