openpyxl 设置活动表 [英] openpyxl Set Active Sheet

查看:260
本文介绍了openpyxl 设置活动表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://openpyxl.readthedocs.io/en/default/_modules/openpyxl/workbook/workbook.html?highlight=set%20active%20sheet

文档显示 Workbook 对象具有 active 属性

The documentation shows a Workbook object has an active property

@property
def active(self):
    """Get the currently active sheet"""
    return self._sheets[self._active_sheet_index]

@active.setter
def active(self, value):
    """Set the active sheet"""
    self._active_sheet_index = value

如果 wb = openpyxl.Workbook() 调用 wb.active 给出默认的第一个工作表的标题,即 Sheet.假设我创建了另一个工作表 ws1 = wb.create_sheet('another sheet'),我如何设置"这是活动工作表?

If wb = openpyxl.Workbook() calling wb.active give the title of the default first worksheet which is Sheet. Say I create another sheet ws1 = wb.create_sheet('another sheet'), how to I "set" this to be the active sheet?

文档显示有一个活动的setter",也称为活动的.它需要一个额外的参数,一个整数索引值.

The documentation shows there is an active "setter" also called active. It takes an extra argument, an integer index value.

为什么 wb.active(1) 不起作用?我不是用

How come wb.active(1) does not work? Am I not calling the function with

推荐答案

这是一种通过工作表名称在工作簿中设置活动工作表的不同方法.也有类似的答案(openpyxl get sheet by name 例如涉及打开一个关闭的文件)而其他人没有足够的细节让我理解这个功能.

This is a different approach of setting active sheet in workbook by sheetname. There are similar answers (openpyxl get sheet by name for example involves opening a closed file) and others did not have enough detail for me to understand this functionality.

在内存中操作工作簿 教程是开始的地方,在答案下我使用本教程来演示没有活动工作表名称,它实际上是活动索引处的工作表.如果添加或删除工作表会更改索引位置的工作表,则会激活不同的工作表.

The Manipulating a workbook in memory tutorial is the place to start and under the answer I have used this tutorial to demonstrate there is no active sheet name, it is actually the sheet at the active index. A different sheet will become active if adding or deleting a sheet changes the sheet at the index position.

最初我认为 .create_sheet 使工作表处于活动状态,但后来我意识到我仅在活动工作表索引处创建了工作表,该索引恰好为 0.索引可以设置为大于工作表的数量和文档还包含一个注释,即如果设置为活动的工作表被隐藏,则返回下一个可见的工作表或无".

Initially I thought .create_sheet made the sheet active but then I realised I had only created the sheet at the active sheet index which happened to be 0. The index can be set to a value greater than the number of sheets and the docs also contain a note that "If the sheet set to active is hidden return the next visible sheet or None".

详细简答

for s in range(len(wb.sheetnames)):
    if wb.sheetnames[s] == 'charlie':
        break
wb.active = s

请随时改进此答案.

演示

(base) C:\Users\User>python
Python 2.7.14 |Anaconda, Inc.| (default, Nov  8 2017, 13:40:45) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> # http://openpyxl.readthedocs.io/en/2.5/tutorial.html#create-a-workbook
...
>>> from openpyxl import Workbook
>>> wb = Workbook()
>>> print(wb.sheetnames)
[u'Sheet']
>>>
>>> print(wb.active)
<Worksheet "Sheet">
>>> ws = wb.active
>>> ws.title = "alpha"
>>>
>>> ws = wb.create_sheet('bravo')
>>> print(wb.sheetnames)
[u'alpha', u'bravo']
>>> print(wb.active)
<Worksheet "alpha">
>>>
>>> ws = wb.create_sheet('charlie',0)  # insert at index 0
>>> print(wb.sheetnames)
[u'charlie', u'alpha', u'bravo']
>>> print(wb.active)
<Worksheet "charlie">
>>>
>>>
>>> wb.active = 1
>>> print(wb.active)
<Worksheet "alpha">
>>>
>>> wb.active = 2
>>> print(wb.active)
<Worksheet "bravo">
>>>
>>> wb.active = 0
>>> print(wb.active)
<Worksheet "charlie">
>>>
>>> wb.active = 3
>>> print(wb.active)
None
>>>
>>> ws = wb.create_sheet(index=0)  # insert at index
>>> print(wb.active)
<Worksheet "bravo">
>>> print(wb.sheetnames)
[u'Sheet', u'charlie', u'alpha', u'bravo']
>>>
>>>
>>> ws_active = wb.get_sheet_by_name('charlie')
__main__:1: DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
>>> ws_active = wb['charlie']
>>> print(wb.active)
<Worksheet "bravo">
>>> ws4 = wb["charlie"] # from https://stackoverflow.com/a/36814135/4539999
>>> print(wb.active)
<Worksheet "bravo">
>>>
>>>
>>> for s in range(len(wb.sheetnames)):
...     if wb.sheetnames[s] == 'charlie':
...         break
...
>>>
>>> wb.active = s
>>>
>>>
>>> print(wb.active)
<Worksheet "charlie">
>>>

这篇关于openpyxl 设置活动表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆