Naming conflict (_FilterDatabase) when feeding Excel with Data from win32com

一个人想着一个人 提交于 2019-12-13 03:20:31

问题


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:

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.

I open the Sheet like this:

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 

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"]
    ...

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.


回答1:


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')



来源:https://stackoverflow.com/questions/58303237/naming-conflict-filterdatabase-when-feeding-excel-with-data-from-win32com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!