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
Related
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
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.
I am writing software that manipulates Excel sheets. So far, I've been using xlrd and xlwt to do so, and everything works pretty well.
It opens a sheet (xlrd) and copies select columns to a new workbook (xlwt)
It then opens the newly created workbook to read data (xlrd) and does some math and formatting with the data (which couldn't be done if the file isn't saved once) - (xlwt saves once again)
However, I am now willing to add charts in my documents, and this function is not supported by xlwt. I have found that xlsxwriter does, but this adds other complications to my code: xlsxwriter only has xlsxwriter.close(), which saves AND closes the document.
Does anyone know if there's any workaround for this? Whenever I use xlsxwriter.close(), my workbook object containing the document I'm writing isn't usable anymore.
Fundamentally, there is no reason you need to read twice and save twice. For your current (no charts) process, you can just read the data you need using xlrd; then do all your processing; and write once with xlwt.
Following this workflow, it is a relatively simple matter to replace xlwt with XlsxWriter.
Morning,
I have dynamic data which is updated either daily, weekly or monthly in excel (this is the only api link). However, for use in python, is it better to keep the data stored in excel or transfer it to SQLite and access it from there?
Or is there a more efficient way of managing this process?
thanks
It depends on what you really need (see below, formulae). KISS (Keep it stupid simple) way is often the good one.
Some Python API like xlwt and xlrd can read and write Excel files :
http://www.python-excel.org/
But xlwt and xlrd can't evaluate formulae. If you need formulae, try openpyxl http://openpyxl.readthedocs.org/en/2.5/
I'm trying to use python to copy data from a dat file to an excel template with openpyxl. I tried doing a few tests to play with a template and found that when I saved the file it deleted most of the existing cells and all of the graphs. I read in another question that openpyxl might not be good for editing existing spreadsheets. Is there a better option or a way to get this one to work?
This is the code I was working on just to see if I could edit the spreadsheet:
import openpyxl
wb=openpyxl.load_workbook('file.xlsm', keep_vba=True)
A=wb.get_sheet_by_name("A")
g=A['F24'].value
print g
A['A1'].value=g
print A['A1'].value
wb.save('file2.xlsm')
When I opened file2 most of the formatting, data, and all the graphs were gone.
Edit:So I'm trying out xlwings and I can't find a good tutorial or list of terms used. Anyone know where I can find that?
This is possible starting with version 2.5 of openpyxl.