I am trying to add Slicer to Pivot Table using Python win32com. I don't understand how to add Slicer.
I've tried various methods but it's not helping.
Related
I am using Python and gspread to upload local .csv data to a google SpreadsheetA.
I have a separate google SpreadsheetB that uses =IMPORTRANGE to import the data from SpreadsheetA and create a pivot table and corresponding chart (both located on SpreadsheetB).
If I were to manually adjust any data in SpreadsheetA (e.g., alter value of any cell, add a value to an empty cell, etc), then the data in SpreadsheetB—with its corresponding pivot table and chart—update dynamically with the new data from SpreadsheetA.
However, when SpreadsheetA is updated with new data programmatically via Python, IMPORTRANGE in SpreadsheetB does not capture the new data.
Any ideas as to why this happens and how I might be able to fix?
Both Sheet A and B show the same number of rows. I am a bit confused with your IMPORTRANGE() formula though, why the ampersand?
=IMPORTRANGE("https://docs.google.com/spreadsheets/d/16DyWC8rsQB1ThpLiQh0p5xH9CYK2cPqbPH547ybw2Fo/edit#gid=1875728384",""&"TestgAPI!A:J")
I changed to this:
=IMPORTRANGE("https://docs.google.com/spreadsheets/d/16DyWC8rsQB1ThpLiQh0p5xH9CYK2cPqbPH547ybw2Fo/edit#gid=1875728384","TestgAPI!A:J")
Although probably not the ideal, my solution to this was to use gspread to add a new worksheet to spreadsheetA, which somehow manages to kickstart importrange() in SpreadsheetB.
I would still love to see a cleaner solution, if anyone knows of one—but this has continued to work since implementing a week ago.
I want to know how one can select an existing table in an excel file using xlwings and resize it to a new value. I only see how to create a new one, but not how to select and manipulate an existing one. If it is not possible, could it be done with openpyxl or other python library?
Thanks in advance!
My Databricks notebook is on Python.
Some codes in the notebook are written in Scala (using the %scala) and one of them is for creating dataframe.
If I use Python/PySpark (the default mode) again, how can I use / access this dataframe that was created when it was on scala mode?
Is it even possible?
Thanks
You can access DataFrames created in one language with another language through temp tables in SparkSQL.
For instance, say you have a DataFarame in scala called scalaDF. You can create a temporary view of that and make it accessible to a Python cell, for instance:
scalaDF.createOrReplaceTempView("my_table")
Then in a Python cell you can run
pythonDF = spark.sql("select * from my_table")
pythonDF.show()
The same works for passing dataframes between those languages and R. The common construct is a SparkSQL table.
Is there a way we can create pivot table in Excel using openpyxl library?
I saw old threads mentioning its not possible, but I saw some documentation about creating one on below link:
https://openpyxl.readthedocs.io/en/2.5/api/openpyxl.pivot.table.html
Although I cant find any practical example of how to use the information provided in above link.
It cannot be done. As mentioned already in the comments and also in the official docs, the pivot module of openpyxl is only there to preserve existing pivots.
I suggest you create a template.xlsx workbook with raw data on one sheet and your pivot table on another one. In the Excel pivot options, you activate refresh data when opening file. Then you use openpyxl to: Load this template, update the raw data and save it somewhere.
Excel will update the pivot table content when the file is opened the next time.
How can I go about creating a worksheet (within an excel workbook) with a pivot table using python libs like pyExcelerator / xlrd? 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".
If by "pivot table" you mean the Excel mechanism, you are out of luck, because that can be created only by 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.
xlrd you appear to know about.
xlwt is a fork of pyExcelerator with bugs fixed and several enhancements. pyExcelerator appears not to be maintained.
xlutils is a package of utility modules. 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.
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.
Try to have a look at this: Python: Refresh PivotTables in worksheet
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.
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. To do this automatically, create a WorkBook_Open event handler.
The VBA code to refresh a pivot table is:
Sheet1.PivotTables(1).PivotCache.Refresh