我最近一直在处理EXCEL文档,在掌握"表格"方面遇到了困难.我认为有些混淆源于这样一个事实,即一些在线资源将工作表中的值数组引用为表,而我对处理表对象(EXCEL中的"插入表")感兴趣.

previous question开始,我感觉到Openpyxl可以识别"表",它似乎能够像CSV一样读入并操作它.然而,在将工作表作为脚本中的变量加载时,我遇到了一个错误:

VBS_Metrics_Report_dir = "path to .xlsx file"
VBS_Metrics_Report = openpyxl.load_workbook(os.path.join(VBS_Metrics_Report_dir, "VBS_Metrics_Report.xlsx"))
VBS_Membership_ws = VBS_Metrics_Report['Membership']
VBS_Membership_table = VBS_Membership_ws.tables['Membership']

---> 192 VBS_Metrics_Report = openpyxl.load_workbook(os.path.join(VBS_Metrics_Report_dir, "VBS_Metrics_Report.xlsx"))

ValueError: Table with name Membership already exists

首先,我感到困惑,因为据说错误所在的行甚至没有try 访问表或名为"Membership"的表,这发生在以下几行中.

其次,该错误断言该表已经存在于何处?考虑到该错误即使在重新启动python内核之后也会发生,它不可能已经作为变量存在于我的工作区中.即使它这样做了,我也应该能够覆盖变量no?

如果问题是表已经存在于我试图访问的工作簿中,那么是的,当然它已经存在,这就是我try 访问它的原因.以利用我所知道的数据.在这种情况下,我一定是误解了load_workbook函数的用法.

我意识到这个问题基本上已经被问到了here个,但它还没有得到真正的回答,我想我应该补充一些 idea .

如果任何人可以帮助我解决这个问题或有洞察力如何处理excel"表"工程给我一个更好的理解,请让我知道.我会非常感激的!

谢谢.

推荐答案

这是有关错误原因的一些其他信息,可能对您有所帮助.

ValueError:"Table with name Membership already exists"是在Openpyxl工作表文件的'add_table()'函数中生成的. 即错误文本只存在于该函数中,因此只有在调用该函数时才会出现.

当您在代码中将表添加到工作表时调用此函数'add_table()',例如,创建表‘Table1’,然后添加到工作表VBS_Membership_ws.table_add(TABLE1).
但是,如果工作簿包含至少具有一个表的任何工作表,它也将由load_workbook调用.
在"Load_Workbook"上,代码将判断工作簿中的每个工作表,如果存在表,则 for each 工作表调用ws.add_table(table),其中TABLE是该表的名称.
这就是在代码行VBS_Metrics_Report = openpyxl.load_workbook(os.path.join(VBS_Metrics_Report_dir, "VBS_Metrics_Report.xlsx"))上产生错误的原因

调用‘ADD_TABLE’时,它将运行重复的表名判断,如果返回True,则引发ValueError.
Worksheet.py

def add_table(self, table):
    ...
    if self.parent._duplicate_name(table.name):
        raise ValueError("Table with name {0} already exists".format(table.name))

在_DUPLICATE_NAME函数中,测试很简单;
Workbook.py

if name == t.lower():

where
'name' is the name of the table to be checked
't' is the name of an existing table
Both 'name' and 't' are set to lowercase so that the check is case insensitive.

There is no check here against the Sheet Name (Title) so having a Table with the same name as the Sheet will not return True and raise the error.

"t"列表在中显示为累积,因 for each 表名都被选中,它被添加到"t"列表以对照下一个表名进行判断.

例如;
如果我在名为‘Table1’、‘Table2’和‘Table3’的工作表上有3个表,那么

  1. "Table1";%t为空,因此不对照任何内容进行判断,"Table1已添加到列表中,并且"%t"现在包含"Table1""
  2. "Table2";根据"Table1"进行判断,然后将"Table2"添加到列表中,因此"t"现在包含"Table1"和"Table2"
  3. 'Table3'; Is checked against 'Table1' and 'Table2'

    Note the 't' list cumulation will be across all sheets since the unique naming is across the workbook. So if there was another Sheet with a table then 't' would contain the three table names for the next Sheet where the first table would be checked against 'Table1', 'Table2' and 'Table3'.

Therefore I can only see the error occurring if in fact two tables are named the same which is not possible to create in Excel or in Openpyxl given this check. However if the Excel file is manually modified or using an app that does not check names before updating perhaps this might happen.

However ultimately as mentioned without the actual workbook itself it's difficult to determine exactly what is happening but you can debug the code yourself to see what is occurring at these points.

Python相关问答推荐

七段显示不完整

跳过包含某些键的字典

在Python中,如何才能/应该使用decorator 来实现函数多态性?

在Python中根据id填写年份系列

在两极中实施频率编码

Matplotlib轮廓线值似乎不对劲

是pandas.DataFrame使用方法查询后仍然排序吗?

Python会扔掉未使用的表情吗?

我在使用fill_between()将最大和最小带应用到我的图表中时遇到问题

为什么默认情况下所有Python类都是可调用的?

用NumPy优化a[i] = a[i-1]*b[i] + c[i]的迭代计算

如何从pandas的rame类继承并使用filepath实例化

为什么抓取的HTML与浏览器判断的元素不同?

在极性中创建条件累积和

所有列的滚动标准差,忽略NaN

改进大型数据集的框架性能

如何使用Numpy. stracards重新编写滚动和?

在Python中计算连续天数

Flash只从html表单中获取一个值

在方法中设置属性值时,如何处理语句不可达[Unreacable]";的问题?