Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-如何在Excel自动筛选中显示选择_Python_Win32com_Autofilter - Fatal编程技术网

Python-如何在Excel自动筛选中显示选择

Python-如何在Excel自动筛选中显示选择,python,win32com,autofilter,Python,Win32com,Autofilter,这可能是一项简单的任务,但就我的一生而言,我找不到解决办法。我有一个excel文档,它有一个表。此表中的所有列都应用了自动筛选。我想做的就是能够为第9列选择自动筛选中的所有条目,并将其存储在数组中。我正在使用Win32Com import win32com.client as win32 working_dir = 'C:\\invoice\\' save_dir = 'C:\\test\\' xl = win32.gencache.EnsureDispatch("Excel.Applica

这可能是一项简单的任务,但就我的一生而言,我找不到解决办法。我有一个excel文档,它有一个表。此表中的所有列都应用了自动筛选。我想做的就是能够为第9列选择自动筛选中的所有条目,并将其存储在数组中。我正在使用Win32Com

import win32com.client as win32

working_dir = 'C:\\invoice\\'
save_dir = 'C:\\test\\'

xl = win32.gencache.EnsureDispatch("Excel.Application")
xl.Visible = True

template_wb = xl.Workbooks.Open(working_dir + 'Settlement report V6-EMPLATE.xlsm')

#Worksheets
orgdata_ws = template_wb.Sheets('Organization Data')
masterdata_ws = template_wb.Sheets('Master Data')

#I want to access the autofilter in column 9 and simply get the contents in the autofilter list and put them in the array
filtercontents = []
thefilter = orgdata_ws.Columns(9).Autofilter
for i in thefilter:
    filtercontents.append(i)     ?????????

我记得我的第一个剧本

你和win32结婚了吗


您正在尝试迭代方法引用Autofilter,而不是其返回值Autofilter。通过添加括号,可以调用该方法。如果没有括号,您只需要引用该方法。

我为任何感兴趣的人找到了它。结果表明,我想要访问的列以及数据透视表中的数据透视字段都被引用了。因此,一旦我能够读取该数据透视字段的内容,我就可以将其导入一个数组,然后使用该数组打印出pdf发票。有一些编码上的奇怪之处,但用setdefaultcoding函数解决了这个问题。代码如下:

import win32com.client as win32
import sys

reload(sys)
sys.setdefaultencoding("UTF-8")

working_dir = 'C:\\invoice\\'
save_dir = 'C:\\test\\'

xl = win32.gencache.EnsureDispatch("Excel.Application")
xl.Visible = True

template_wb = xl.Workbooks.Open(working_dir + 'Settlement report V6- TEMPLATE.xlsm')

#Worksheets
settlements_ws = template_wb.Sheets('Settlement')
orgdata_ws = template_wb.Sheets('Organization Data')
masterdata_ws = template_wb.Sheets('Master Data')

settlements_ws.Activate()

agencies = []

def maxrow(sheet):
    used = sheet.UsedRange
    nrows = used.Row + used.Rows.Count - 1
    return nrows

mypivot = settlements_ws.PivotTables("PivotTable2").PivotFields("AgencyName")

for j in mypivot.PivotItems():
    j = str(j)
    if j == "#N/A":
        continue
    else:
        j = j.replace("\xc2\xa0","")
        agencies.append(j)
print agencies

#Looping through agencies and saving PDFs
for i in agencies:
    settlements_ws.Cells(8,3).Value = i
    print settlements_ws.Cells(8,3).Value
    settlements_ws.ExportAsFixedFormat(0, save_dir + i + '.pdf')

print "Finished!"

你得到了什么异常/错误?嗨,拉斐尔,有了这个特定的代码,我收到了这个错误:TypeError:“instancemethod”对象是不可编辑的。但我很可能没有使用正确的方法来实现我的目标。另外,快速更新我将Autofilter更改为Autofilter:您是否缺少第17行的尾随括号:。Autofilter而不是。Autofilter Hey Jeremy谢谢您的提示。是的,我忘了那个。我认为我的概念是有缺陷的,因为我现在得到这个错误,Range类的AutoFilter方法失败了。我正在尝试对已经筛选了列的现有表应用自动筛选,所以可能这就是问题所在。或者,我可能无法以编码的方式迭代自动过滤器。谢谢你的帮助;我可能可以从这些链接中窃取一些东西。我们最好的办法是读取现有的工作表,然后以python代码的形式运行过滤器,并将结果输出到新的excel工作表。根据我的经验,阅读很容易,写作也很容易,但是读和写同一个复杂的excel文件是对Hey Raphael的挑战,谢谢你的帖子。是的,我忘了那个。我现在遇到了一个不同的错误,但是Range类的AutoFilter方法失败了,所以我认为我以这种方式迭代AutoFilter的概念是不正确的。我正在尝试筛选的列上已经有一个预先存在的筛选器,它是表的一部分,因此如果我可以点击该筛选器并提取将实现我的目标的条件!