简体   繁体   English

使用pyExcelerator / xlrd进行数据透视

[英]pivots using pyExcelerator/xlrd

How can I go about creating a worksheet (within an excel workbook) with a pivot table using python libs like pyExcelerator / xlrd? 我该如何使用pyExcelerator / xlrd等python库在数据透视表中创建工作表(在excel工作簿中)? I need to generate a daily report that has a pivot table to summarize data on other sheets. 我需要生成一个包含枢纽分析表的每日报告,以汇总其他工作表上的数据。 One option would be to have a blank template that I copy and populate with the data. 一种选择是让我复制并填充数据的空白模板。 In this case, is there a way to refresh the pivot from code? 在这种情况下,是否有办法从代码中刷新数据透视表? Any other suggestions? 还有其他建议吗?

Please clarify (by editing your question) whether "sheet" is an abbreviation of "spreadsheet" and means a whole XLS file, or whether it's an abbreviation of "worksheet", a component of a "workbook". 请澄清(通过编辑问题)“工作表”是“电子表格”的缩写,是整个XLS文件的意思,还是“工作表”的缩写,“工作表”的组成部分。

If by "pivot table" you mean the Excel mechanism, you are out of luck, because that can be created only by Excel. 如果用“数据透视表”表示Excel机制,那么您很不走运,因为只能由Excel创建。 However if you mean a "cross-tab" that you create your self using Python and an appropriate library, you can do this using the trio of xlrd, xlwt and xlutils. 但是,如果您的意思是使用Python和适当的库创建自己的“交叉表”,则可以使用xlrd,xlwt和xlutils三者来完成此操作。

xlrd you appear to know about. 您似乎知道的xlrd。

xlwt is a fork of pyExcelerator with bugs fixed and several enhancements. xlwt是pyExcelerator的一个分支,已修复了一些错误,并进行了一些增强。 pyExcelerator appears not to be maintained. pyExcelerator似乎未得到维护。

xlutils is a package of utility modules. xlutils是实用程序模块的软件包。 xlutils.copy can be used to make an xlwt Workbook object from an xlrd Book object, so that you can make changes to the xlwt Workbook and save it to a file. xlutils.copy可用于从xlrd Book对象创建xlwt Workbook对象,以便您可以对xlwt Workbook进行更改并将其保存到文件中。

Here is your one-stop-shop for more info on the three packages , together with a tutorial, and links to a google-group/mailing-list which you can use to get help. 这是您的一站式商店,以获取有关这三个软件包的更多信息 ,以及一个教程,以及指向可用于获取帮助的google-group / mailing-list的链接。

Try to have a look at this: Python: Refresh PivotTables in worksheet 尝试看一下: Python:刷新工作表中的数据透视表

If you figure out howto create the pivot tables then you can use my code to refresh them 如果您知道如何创建数据透视表,则可以使用我的代码刷新它们

I do not believe you can programatically add a pivot table using xlwt . 我不相信您可以使用xlwt编程方式添加数据透视表。

But your second approach (populating a pre-configured workbook) seems reasonable. 但是您的第二种方法(填充预先配置的工作簿)似乎是合理的。

You can refresh the pivot table using a VBA macro in the template workbook. 您可以使用模板工作簿中的VBA宏刷新数据透视表。 To do this automatically, create a WorkBook_Open event handler. 若要自动执行此操作,请创建一个WorkBook_Open事件处理程序。

The VBA code to refresh a pivot table is: 刷新数据透视表的VBA代码为:

Sheet1.PivotTables(1).PivotCache.Refresh

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

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