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
Related
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
After parsing Excel file to Python and evaluating the workbook using pycel, can the pycel object be saved as an Excel file maintaining all original formatting, etc? I.e. only values need to be updated.
TL;DR
No, you cannot save a pycel object back into Excel.
Why not?
The basic problem is that pycel is based on openpyxl. Openpyxl is used to read (and write if needed) Excel spreadsheets. However, while openpyxl has the computed values available for formula cells for a workbook it read in, it does not really allow those computed values to be saved back into a workbook it writes. It doesn't really make sense to save a different computed value for a formula cell, since the cell's value will be recomputed once it is opened back up in Excel.
While it is true that pycel has the information available to properly populate a new value when the workbook is written, it evidently is not a use case that was important to the openpyxl authors or contributors.
Please note that the openpyxl maintainers gladly took pull requests to make it run better with pycel. It seems likely they would be open to discussing a PR for writing values into workbooks.
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.
I got an excel file that has four sheets. One sheet, sheet 4. contains data in simple CSV and the others read the data of this sheet and make different calculations and graphs. In my python application I would like to open the excel file, open sheet 4, and replace the data. I know you technically can't open and edit excel however you like with Python, due to the complex file structure of XLS (previous relevant answer), but is there a work around for this specific case? Remember the only thing I want to do is to open the data sheet, write to it, and ignore the others...
Note: Previous answers to relevant questions have suggested using the copy function in xlutils. But that doesn't work in this case, as the rest of the sheets are rather complex. The graphs, for example, can't be preserved with the copy function.
I used to use pyExcelerator. It did certainly a good job, but I'm not sure if it is maintained.
https://pypi.python.org/pypi/pyExcelerator/
hth.
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