0
votes

The online version of Office 365 seems to have stopped recognising colour and other formatting from openpyxl. A simple test case:

from openpyxl import Workbook
from openpyxl.styles import PatternFill
yellow = PatternFill(start_color='00FFFF00',
                   end_color='00FFFF00',
                   fill_type='solid')
wb = Workbook()
ws = wb.active
ws['A1'] = 1
ws['A1'].fill = yellow
wb.save('openpyxl_test.xlsx')
wb.close()

generates an Excel file with a yellow background to cell A1, containing a "1". This displays fine in the Excel app under Windows10, but open it in the online Office 365 version of Excel and the yellow is gone, never to return. This also happens if an existing Excel file is simply read-in and written-out through openpyxl: the stand-alone version of Excel has no problems with the result but the web version kills the colour. If the same file has not been through openpyxl then the web version displays it fine, with colour. This behaviour seems to be new (the last couple of days), as I've files from last week from openpyxl that online Excel digested fine. I feel I'm missing something, but can't think what. These tests are with openpyxl 3.0.5.

update: This seems to be account related and only with my work Office365 account. When others upload the identical file to the same folder, colour is preserved. That is: two users uploading the identical file to the same place results in different behaviour when it is opened by anyone. If I upload it to my personal Office365 OneDrive (also through the web interface) rather than work then it's fine too and colour is preserved. This only affects files that have at some point passed through openpyxl and that have been first opened after two days ago.

1
I can test with Office 365 but this sounds like bug in it. - Charlie Clark
I agree -- probably something to do with a change to the file owner's profile interacting with the openpyxl version of the formatting style. I'd guess this is not a common problem! - Graham Woan
There isn*t an openpyxl version of the style: it's covered by the OOXML specification. Hence, what you're seeing is a bug in Office 365. - Charlie Clark
Thanks Charlie. Agreed there shouldn't be, but openpyxl simply has to open and save an existing file for Office 365 to fail (if and only if I own it). So either openpyxl or O365 is not compliant, or the ownership profile is messing with the changes openpyxl makes to the formatting. It's not entirely clear to me yet that MS are the culprits (though it seems increasingly likely) as everything is fine if openpyxl is not in the loop. - Graham Woan
You can verify the file that openpyxl produces with the OOXML tools. - Charlie Clark

1 Answers

0
votes

Some headway: I made a file in desktop excel, ran it through openpyxl (just read/write), and then through desktop excel again (read/write). The final version is coloured when opened online. Examining it with OOXML Tools, I see that styles.xml contains an element

<cellXfs count="2">
   <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
   <xf numFmtId="0" fontId="0" fillId="2" borderId="0" xfId="0" applyFill="1"/>

whereas the corresponding element in the version straight from openpyxl is

  <cellXfs count="2">
   <xf numFmtId="0" fontId="0" fillId="0" borderId="0" pivotButton="0" quotePrefix="0" xfId="0"/>
   <xf numFmtId="0" fontId="0" fillId="2" borderId="0" pivotButton="0" quotePrefix="0" xfId="0"/>

I can make this version coloured by adding applyFill="1" to the faulty file, ie

  <cellXfs count="2">
   <xf numFmtId="0" fontId="0" fillId="0" borderId="0" pivotButton="0" quotePrefix="0" xfId="0"/>
   <xf numFmtId="0" fontId="0" fillId="2" borderId="0" pivotButton="0" quotePrefix="0" xfId="0" applyFill="1"/>

So this seems to be a fix that Excel does to the openpyxl-generated file, but it doesn't do it online if I'm the guy who uploads it!