向 Excel 提供来自 win32com 的数据时命名冲突(_FilterDatabase) [英] Naming conflict (_FilterDatabase) when feeding Excel with Data from win32com

查看:36
本文介绍了向 Excel 提供来自 win32com 的数据时命名冲突(_FilterDatabase)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个快速脚本,从标准化表单(word docs/docxs)中提取数据并将它们传输到 Excel 工作表中,并附上一个可以进行一些计算的工作表.一切都很好,只有一个例外:

I wanted to create a quick script that pulls data from standardized forms (word docs/docxs) and transports them into an Excel Sheet, with an attached sheet that does some calculating. Everything works pretty well, with one exception:

我希望能够过滤特定项目编号、用户姓名首字母或其他数据点的结果,但是当我在 Excel 工作表上添加过滤器时,每当我从 Python 运行它时,我都会收到命名冲突"错误消息e并且必须为_FilterDatabase"选择一个新名称 - 当我这样做时,过滤器消失了.如果我不这样做,脚本就会崩溃.

I want to be able to filter the results for specific project numbers, user initials or other datapoints, but when I add a filter over the Excel sheet, whenever I run it from Python, I get a "Naming conflict" error messagee and have to choose a new name for "_FilterDatabase" - when I do, the filter is gone. If I don't, the script crashes.

我这样打开工作表:

import win32com.client as win32
def openExcel():
    xl = win32.gencache.EnsureDispatch('Excel.Application')
    wb = xl.Workbooks.Open(path+"\\"+'Analysis.xlsx')
    ws = wb.Sheets(1)
    xl.Visible = True

    return ws 

然后我将数据中包含的数据点传递给 Excel

Then I pass the datapoints contained in data to Excel

def print2Excel(data, ws):
    const = win32.constants
    ws.Range("A2:H2").Insert(const.xlShiftDown, const.xlFormatFromRightOrBelow)
    ws.Cells(2,1).Value = data["datapoint"]
    ...

这个问题仍然存在,即使我事后移除过滤器也是如此.一旦 Excel 工作表被过滤一次,错误就会发生,直到时间结束.到目前为止唯一的出路是git reset/checkout Excel.

This Problem persists, even when I remove the filter after the fact. As soon as the Excel sheet was filtered once, the error will occur until the end of time. The only way out so far was to git reset/checkout the Excel.

推荐答案

我看不到您在哪里应用过滤器,但在应用过滤器之前,您是否尝试将 AutoFilterMode 设置为 False 然后添加过滤器?例如

I couldn't see where you apply the filter but before you apply the filter have you tried turning the AutoFilterMode to False then add the filter? For example

    sh_data.Rows(1).AutoFilter(Field=1, Criteria1='B')

这篇关于向 Excel 提供来自 win32com 的数据时命名冲突(_FilterDatabase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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