且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

启动Excel时,如何使用openpyxl将Excel工作表中的所有列折叠全部隐藏或显示?

更新时间:2023-10-04 17:51:52

对于您的问题并不是一个很好的答案,但这是我对从Google表格生成的.xlsx文件进行源分割时发现的内容.首先,您使用 unzip 解压缩.xlsx文件,然后进入 xl/worksheets/sheet1.xml 或等效文件.

Not quite an answer to your question but here is what I found when source-diving an .xlsx file generated from google sheets. First you unzip the .xlsx file using unzip then you go into xl/worksheets/sheet1.xml or equivalent.

假设我要创建三个嵌套的分组列的大纲级别:

Let's say I want to create three nested outline level of grouped columns:

  • 最顶层(最上层)来自 6:40

第二级(中间)来自 6:13

第三级(底部)来自 6:8

,它们跨越6-40列.(列号从'A'开始为1索引).这是xlsx文件中的代码:

and they span columns 6-40. (Column numbers are 1-indexed starting at 'A'). Here is the code in the xlsx file:

<cols>
  <col min="6" max="8" outlineLevel="3"/>
  <col min="9" max="13"  outlineLevel="2"/>
  <col min="14" max="40" outlineLevel="1"/>
</cols>

一些注意事项:

  • 列组永远不会重叠!如果这样做,它们会自动合并,这是不直观的.
  • 最底层的级别被编号为最高.最重要的是outlineLevel#1
  • 上方"大纲级别(较低的数字)将吞下"较低的.所以outlineLevel 1最终会跨越 6:40 ,而outlineLevel 2将会跨越 6:13
  • Column groups never overlap! If they do they are automatically merged, which is non-intuitive.
  • The bottom most level is numbered the highest. The top one is outlineLevel #1
  • The "above" outline levels (lower numbered) will "swallow" the lower ones. So outlineLevel 1 will end up spanning 6:40 and outlineLevel 2 will span 6:13

然后是生成的openpyxl代码:

Here is then the resulting openpyxl code:

ws.column_dimensions.group(get_column_letter(6), get_column_letter(8),outline_level=3)
ws.column_dimensions.group(get_column_letter(9), get_column_letter(13),outline_level=2)
ws.column_dimensions.group(get_column_letter(14), get_column_letter(40),outline_level=1)

我将查看是否可以添加到 https://***.com/users的软件包文档中/2385133/charlie-clark .这纯粹是xlsx规范(或行为)的细微差别,而不是特定于openpyxl的.但是,***在API中提供示例.

I'll see if I can get something to add to the package documentation for https://***.com/users/2385133/charlie-clark. This is purely a nuance of the xlsx spec (or behavior), not openpyxl specific. But it would be nice to have the example available in the API.

如果您对行而不是列感兴趣,请在这里查看我的答案: https://***.com/a/67203916/7018268

If you are interested in rows instead of columns, see my answer here : https://***.com/a/67203916/7018268