Create Pivot Table using Openpyxl - python

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.

Related

Python creating excel files

im trying to generate a excel while not deleting the user configuration.
For example you can create here
a new view. And save it.
But when im reading the excel file with pandas or anything else and generate the excel 'the view' would be deleted.
Is there a way where I can create the view in python again? Or dont delete the view?
I looked into some other libraries like openpyxl, xlswriter, but i didnt found any option that can do this.
Openpyxl has the functionality to use Sheet Views. I've never used it, so I can't give you specifics. In theory it would allow you to read and rebuild a Sheet View.
Pandas doesn't include that functionality as far as I know. What it does have is the recent ability (and it's also in openpyxl) to append to an existing Excel workbook instead of overwriting.
If you have a Sheet View pointing at a particular sheet, and are adding/editing a different sheet, you could use this and it shouldn't impact the sheet view.
If you are editing the sheet the sheet view is pointing at, then you would need to rebuild the view using Openpyxl (but you could still write to it initially with Pandas if that is easier for you).
The code for appending in Pandas is:
# use ExcelWriter rather than using to_Excel directly in order to give access to the append & replace functions
with pd.ExcelWriter("data.xlsx", engine="openpyxl", mode="a", if_sheet_exists="replace") as writer:
df.to_excel(writer, 'My Data', index=False)
If you are using openpyxl directly, then workbook.create_sheet(sheet_name) will append a new sheet to an existing workbook.
You may find that you have to use win32com, a module which gives you access to some of the functionality that vba has in Excel. The documentation for Views seems scarce though; all I could easily find where these two:
https://learn.microsoft.com/en-us/office/vba/api/excel.window.sheetviews
https://learn.microsoft.com/en-us/office/vba/api/excel.sheetviews

How to write table to different sheet in openpyxl

I tried using Pandas but am getting errors that I don't understand quite yet when trying to import the file. I am able, however, to import the file easily using openpyxl.
I have a very large sheet that has header data. It also contains a table that I'll need to transpose up towards the top and then the main table starts on a row (39). I am able to import the sheet and then run:
sheet_obj = wb_obj.active
sheet_obj.delete_rows(1,39)
I want to be able to write the new object to a new sheet (I called "test") in the same work book so that I can test what I'm deleting. (Eventually, I will be exporting this to MySQL, but wanting to see the contents of the table as sort of a debugging effort. I am unable to figure out how to write the sheet_obj to the other sheet. I'm sure this is simple....
Two questions:
How to write a sheet object to a NEW sheet
Is there a simple way to transpose the object? (I saw that pandas has a wb.T method - does openpyxl have something similar?
THANK YOU SO much! I'm very new to python and learning all of this on the fly.
Sincerely,
Rob

How to selectively update one or more sheets of same Excel workbook using python pandas retaining formulae?

Went through some trial-n-error using online documentation (xlrd, etc) but having no luck that accomplishes all of the below:
I have 1 xlsx workbook with several sheets and each sheet has several cells with static values and formulae. I want to read these sheets into pandas, generate new dataframes and then update the same workbook. However, I want to update only certain cells and in a way that retains the formula of the corresponding cells after the update. I want to update the same workbook and not create a new excel file.
What's the most reliable way to accomplish this? Any guidelines would be much appreciated. Thank You.

Opening a pdf and reading in tables with python pandas

Is it possible to open PDFs and read it in using python pandas or do I have to use the pandas clipboard for this function?
you can use tabula
https://blog.chezo.uno/tabula-py-extract-table-from-pdf-into-python-dataframe-6c7acfa5f302
from tabula import read_pdf
df = read_pdf('data.pdf')
I can see more in the link!
There is a new version of tabula called tabula-py
pip install tabula-py
the .read_pdf method works just like in the old version, documentation is here:
https://pypi.org/project/tabula-py/
In case it is a one-off, you can copy the data from your PDF table into a text file, format it (using search-and-replace, Notepad++ macros, a script), save it as a CSV file and load it into Pandas.
If you need to do this in a scalable way, you might try this product: http://tabula.technology/. I have not used it yet, so I don't know how well it works, but you can explore it if you need it.
I have been doing some tests with Camelot (https://camelot-py.readthedocs.io/en/master/), and it works very good in many situations. And you can try to adjust some parameters if the default ones doesn't work.
It's similar to Tabula, but it use different algorithms (Tabula use the vector data in the PDF and raster the lines of the table; Camelot uses Hough Transform), so you can try both to find the best one.
Both have a web version, so you can try with some example to decide which is the best one for your application.
this is not possible. PDF is a data format for printing. The table structure is therefor lost. with some luck you can extract the text with pypdf and guess the former table columns.
Copy the table data from a PDF and paste into an Excel file (which usually gets pasted as a single rather than multiple columns). Then use FlashFill (available in Excel 2016, not sure about earlier Excel versions) to separate the data into the columns originally viewed in the PDF. The process is fast and easy. Then use Pandas to wrangle the Excel data.
I use Tabula library
for install, via:
pip install tabula-py
reading several tables inside PDF by link , example:
import tabula
df = tabula.io.read_pdf(url, pages='all')
then you will get many tables, you can call it by using index, it's like printing element from list, Example:
# ex
df[0]
more info here - https://pypi.org/project/tabula-py/

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? 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

Categories