英文:
How can I add multiple non-consecutive cells to Name Manager in Excel using Python?
问题
我正在尝试使用Python在Excel名称管理器中的一个名称中添加多个单元格,这些单元格不总是连续的。您可以在附加的截图中看到我想要做的示例。
我尝试过Python库openpyxl和XlsxWriter,但是这两个库只能定义一个特定的单元格或一个特定的范围。
示例
- 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.
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("test", "=Sheet1!$B$3:$E$8,Sheet1!$B$12:$C$16,Sheet1!$H$12:$I$16,Sheet1!$B$19")
Xlwings:<br>
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:<br>
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"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论