简体   繁体   English

使用openpyxl对Excel进行排序和自动过滤

[英]Sorting and auto filtering Excel with openpyxl

I am trying to sort a spreadsheet using openpyxl and Python. 我正在尝试使用openpyxl和Python对电子表格进行排序。 I have read the documents and I don't quite understand this page. 我已经阅读了这些文件,我对此页面并不十分了解。 I am expecting it to either add the auto filter dropdown arrows or sort my spreadsheet and it is returning errors. 我希望它能够添加自动过滤器下拉箭头或对电子表格进行排序,并返回错误。 Here's my code 这是我的代码

wb = openpyxl.load_workbook('report.xlsx')
ws = wb.active
ws['A2'] = "Store"
ws['B2'] = "Manager"
ws['C2'] = "Zone"
ws.column_dimensions.group('F','DU',hidden=True)
#ws.AutoFilter.add_sort_condition('C:C')
wb.save("report.xlsx")

According to the documents it looks like the line "ws.AutoFilter.add_sort_condition('C:C')" should give me the result I want. 根据文件,它看起来像“ws.AutoFilter.add_sort_condition('C:C')”这一行应该给我我想要的结果。 (Yes I understand it is currently a comment line. The rest of my code runs fine without that line so I commented it.) (是的,我知道它目前是一个注释行。我的其余代码在没有该行的情况下运行正常,所以我对它进行了评论。)

When I have that line in the code I get the error - 'Worksheet' object has no attribute 'AutoFilter' but according to the documents it looks like it does. 当我在代码中有那行时我得到错误 - 'Worksheet'对象没有属性'AutoFilter'但是根据它看起来像它的文件。 http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/worksheet/filters.html#AutoFilter . http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/worksheet/filters.html#AutoFilter

If anyone can help explain to me why it is failing or what the documents mean that would be great. 如果有人可以帮我解释为什么它失败或文件意味着什么会很好。

This statement in the documents is particularly confusing to me: 文件中的这句话对我来说特别困惑:

"Don't create auto filters by yourself. It is created by :class: ~openpyxl.worksheet.Worksheet . You can use via :attr: ~~openpyxl.worksheet.Worksheet.auto_filter attribute." “不要自己创建自动过滤器。它是由:class: ~openpyxl.worksheet.Worksheet创建的。你可以使用via:attr: ~~openpyxl.worksheet.Worksheet.auto_filter属性。”

because I tried that too and it also failed. 因为我也尝试了它也失败了。

Update: @crussell's reply worked in that it added the auto filter to my spreadsheet. 更新:@ crussell的回复有效,它将自动过滤器添加到我的电子表格中。 However, it is still not adding the sort condition to the appropriate column. 但是,它仍然没有将排序条件添加到适当的列。

See here: http://openpyxl.readthedocs.org/en/latest/api/openpyxl.worksheet.html?highlight=auto_filter#openpyxl.worksheet.worksheet.Worksheet.auto_filter 请参见: http//openpyxl.readthedocs.org/en/latest/api/openpyxl.worksheet.html?highlight = auto_filter#openpyxl.worksheet.worksheet.Worksheet.auto_filter

The auto_filter command returns the AutoFilter object, so in a sense they are the same thing. auto_filter命令返回AutoFilter对象,因此在某种意义上它们是相同的。

What you need is ws.auto_filter.ref = 'C1:C20' with the range of cells those of which you want to filter. 您需要的是ws.auto_filter.ref = 'C1:C20' ,其中包含您要过滤的单元格范围。

According to the documentation openpyxl can define the filter and/or sort but does not apply them! 根据文档,openpyxl可以定义过滤器和/或排序,但不适用它们!

They can only be applied from within Excel 它们只能在Excel中应用

I don't have a full answer for this, but I did find something interesting when using filters. 我没有完整的答案,但我确实在使用过滤器时发现了一些有趣的东西。 I added a filter column, eg: ws.auto_filter.add_filter_column(1,{},True) 我添加了一个过滤列,例如: ws.auto_filter.add_filter_column(1,{},True)

then I opened the resulting spreadsheet. 然后我打开了生成的电子表格。 The column showed the filter! 该栏显示了过滤器! But the data was not actually filtered in the spreadsheet. 但数据实际上并未在电子表格中过滤。 I had to click on the "Data" tab and click "Reapply" the filter. 我必须单击“数据”选项卡,然后单击“重新应用”过滤器。

So it looks like the adding of a sort or filter column works, except it never actually applies the sort or filter. 所以看起来添加排序或过滤列似乎有效,除非它实际上从未应用排序或过滤器。

I have been hunting down a function that will apply the filter, but have not yet had any luck. 我一直在寻找一个将应用过滤器的功能,但还没有任何运气。 If anyone has thoughts, I'd love to hear them! 如果有人有想法,我很乐意听到他们!

Thanks! 谢谢!

After looking at the documentation and the source code for the AutoFilter.add_sort_condition() function it looks like the ref you're providing may need to be changed to include row indices, like "C1:C120", for example. 在查看AutoFilter.add_sort_condition()函数的文档和源代码之后,看起来您提供的ref可能需要更改为包括行索引,例如“C1:C120”。 Have you tried it with specific row numbers? 您是否尝试过具体的行号? Also, be sure to take a look at the comment regarding the ref variable right below the function declaration in: 另外,请务必查看函数声明正下方关于ref变量的注释:

http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/worksheet/filters.html#AutoFilter http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/worksheet/filters.html#AutoFilter

if you're not following where I'm coming from. 如果你不跟随我来自哪里。 Cheers and good luck! 干杯,祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM