如何使用Python将多个不连续的单元格添加到Excel中的名称管理器?

huangapple go评论78阅读模式
英文:

How can I add multiple non-consecutive cells to Name Manager in Excel using Python?

问题

我正在尝试使用Python在Excel名称管理器中的一个名称中添加多个单元格,这些单元格不总是连续的。您可以在附加的截图中看到我想要做的示例。

我尝试过Python库openpyxlXlsxWriter,但是这两个库只能定义一个特定的单元格或一个特定的范围。

示例

  • openpyxl
specific_cell = DefinedName('specific_cell', attr_text='Sheet1!$C$8')
specific_range = DefinedName('specific_range', attr_text='Sheet1!$C$8:$J$13')
  • XlsxWriter
workbook.define_name('specific_cell', '=Sheet1!$G$1')
workbook.define_name('specific_range', '=Sheet1!$G$1:$H$10')

是否有办法在名称管理器中添加比上述更复杂的内容?

根据附加的截图,类似以下的方式:

workbook.define_name('complex_range','=Sheet1!$B$3:$E$8;Sheet1!$B$12:$C$16;Sheet1!$B$19;Sheet1!$H$12:$I$16')

请注意,由于代码部分不要求翻译,我只提供了翻译后的文本部分。

英文:

I am trying to add multiple cells, which are not always consecutive, in only one Name in Excel Name Manager using Python. You can see an example of what I want to do in the attached screenshot.

如何使用Python将多个不连续的单元格添加到Excel中的名称管理器?

I have tried the Python libraries openpyxl and XlsxWriter, but both libraries can only define a specific cell or a specific range.

Examples

  • openpyxl
specific_cell = DefinedName('specific_cell', attr_text='Sheet1!$C$8')
specific_range = DefinedName('specific_range', attr_text='Sheet1!$C$8:$J$13')
  • XlsxWriter
workbook.define_name('specific_cell', '=Sheet1!$G$1')
workbook.define_name('specific_range', '=Sheet1!$G$1:$H$10')

Is there any way to add to Name Manager something more complicated than the above?

Based on the attached screenshot something like

workbook.define_name('complex_range','=Sheet1!$B$3:$E$8;Sheet1!$B$12:$C$16;Sheet1!$B$19;Sheet1!$H$12:$I$16')

答案1

得分: 2

Xlsxwriter:

workbook.define_name("test", "=Sheet1!$B$3:$E$8,Sheet1!$B$12:$C$16,Sheet1!$H$12:$I$16,Sheet1!$B$19")

Xlwings:

workbook.names.add(name="test", refers_to="=Sheet1!$B$3:$E$8,Sheet1!$B$12:$C$16,Sheet1!$H$12:$I$16,Sheet1!$B$19")

Openpyxl:

workbook.defined_names.add(DefinedName("test", attr_text="Sheet1!$B$3:$E$8,Sheet1!$B$12:$C$16,Sheet1!$H$12:$I$16,Sheet1!$B$19"))
英文:

These will work for each module, the common factor being comma rather than semi-colon?<br>
<br>

Xlsxwriter:<br>

workbook.define_name(&quot;test&quot;, &quot;=Sheet1!$B$3:$E$8,Sheet1!$B$12:$C$16,Sheet1!$H$12:$I$16,Sheet1!$B$19&quot;) 

Xlwings:<br>

workbook.names.add(name=&quot;test&quot;, refers_to=&quot;=Sheet1!$B$3:$E$8,Sheet1!$B$12:$C$16,Sheet1!$H$12:$I$16,Sheet1!$B$19&quot;) 

Openpyxl:<br>

workbook.defined_names.add(DefinedName(&quot;test&quot;, attr_text=&quot;Sheet1!$B$3:$E$8,Sheet1!$B$12:$C$16,Sheet1!$H$12:$I$16,Sheet1!$B$19&quot;))

huangapple
  • 本文由 发表于 2023年6月1日 17:34:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定