简体   繁体   English

Openpyxl、颜色和 Office 365

[英]Openpyxl, color and Office 365

The online version of Office 365 seems to have stopped recognising colour and other formatting from openpyxl. Office 365 的在线版本似乎已停止从 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".生成一个 Excel 文件,单元格 A1 具有黄色背景,包含“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.这在Windows10下的Excel应用程序中显示正常,但是在Excel的在线Office 365版本中打开它,黄色消失了,永远不会回来。 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.如果通过 openpyxl 简单地读入和写出现有的 Excel 文件,也会发生这种情况:Excel 的独立版本对结果没有问题,但 Z2567A5EC9705EB7AC2C984033E0618 版本杀死了颜色If the same file has not been through openpyxl then the web version displays it fine, with colour.如果同一个文件没有通过 openpyxl,那么 web 版本可以很好地显示它,并带有颜色。 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.这种行为似乎是新的(最近几天),因为我上周从 openpyxl 获得了在线 Excel 消化良好的文件。 I feel I'm missing something, but can't think what.我觉得我错过了一些东西,但想不出什么。 These tests are with openpyxl 3.0.5.这些测试使用 openpyxl 3.0.5。

update: This seems to be account related and only with my work Office365 account.更新:这似乎与帐户相关,并且仅与我的工作 Office365 帐户有关。 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.如果我将它上传到我的个人 Office365 OneDrive(也通过 web 接口)而不是工作,那么它也很好并且颜色得以保留。 This only affects files that have at some point passed through openpyxl and that have been first opened after two days ago.这仅影响在某些时候通过 openpyxl 并且在两天前首次打开的文件。

Some headway: I made a file in desktop excel, ran it through openpyxl (just read/write), and then through desktop excel again (read/write).一些进展:我在桌面 excel 中创建了一个文件,通过 openpyxl 运行它(只是读/写),然后再次通过桌面 excel (读/写)。 The final version is coloured when opened online.在线打开时,最终版本是彩色的。 Examining it with OOXML Tools, I see that styles.xml contains an element使用 OOXML 工具检查它,我看到 styles.xml 包含一个元素

<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而直接来自 openpyxl 的版本中的相应元素是

  <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我可以通过将applyFill="1"添加到错误文件来使这个版本着色,即

  <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!所以这似乎是 Excel 对 openpyxl 生成的文件所做的修复,但如果我是上传它的人,它不会在网上进行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM