OpenPyXl Workbook主题颜色不是Excel的标准
我正在尝试使用Python和OpenPyXl将单元颜色从一个XLSX复制到另一种XLSX。使用:
cellInNewBook.fill = copy(cellInOriginalBook.fill)
执行此操作,但由于某种原因,我创建的工作簿使用:
newBook = Workbook()
在Excel中打开它时具有不同的主题颜色。
当我选择在原始XLSX中编辑单元背景颜色时,它显示了这些主题颜色:
但是当我查看时新工作簿中Excel中相同的单元背景下拉选项它们不同,因此我的副本显示了不同的颜色,因为每个颜色的索引不同:
所以我得出的结论是,主题颜色是由OpenPyXl的Workbook()设置的,它们不是Excel所拥有的默认值。有没有一种方法可以使新工作簿与原始XLSX工作簿相同的顺序/位置匹配主题颜色?
I am trying to copy cell colors from one xlsx to another using python and openpyxl. Using:
cellInNewBook.fill = copy(cellInOriginalBook.fill)
works for doing this but for some reason the workbook I created using:
newBook = Workbook()
has different Theme Colors when I open it in Excel.
When I choose to edit a cell background color in my original xlsx it shows these Theme Colors:
However when I look at the same cell background dropdown options in Excel for the new workbook they are different, and as a result my copy is showing different colors because the index for each is different:
So I am concluding that the theme colors are being set by openpyxl's Workbook() and they are not the default that Excel would have. Is there a way to make the new Workbook match the Theme colors in the same order/positions as the original xlsx workbook?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OpenPyXl将“主题颜色”和“标准颜色”视为单独的属性。
您可以通过做:
这将显示出长的输出,其中包括与:相似的零件:
注意到RGB最终没有键入的是'主题'而不是'rgb'。
如果您希望使用“主题”而不是标准(RGB)颜色,则需要定义一个提取主题和色调的函数,然后将它们作为字符串或字典项目一起存储在一起。
OpenPyXl treats 'theme color' and 'standard color' as separate attributes.
You can test this for yourself by doing:
This will show a long output including parts that looks similar to:
Notice how rgb is None but type at the end is 'theme' instead of 'rgb'.
If you wish to utilize 'theme' instead of standard (rgb) color, you'll need need to define a function that extracts the theme and tint and then stores them together as a string or a dictionary item.