How to persist shapes in python - python

I have an excel form with shapes. If I change only the sheet contents to Python without changing this form and save it, all the shapes will disappear.
I'm using openpyxl.
How to persist excel shapes in python?

I think you're asking how to encode an Excel file's shapes in openpyxl data, and your goal is to have that data persist when you change the openpyxl data back into an Excel file format.
openpyxl is centered on transferability of data, data visualizations (e.g. charts), and some visual formatting information between Excel and python. But there is an openpyxl.chart.shapes submodule that may provide what you need.

I think you need "xlwings".
xlwing does a lot of things that openpyxl doesn't work. Keep the shapes you want or keep the formulas... However, be careful as it cannot operate in an environment without Excel.
import xlwings
app = xlwings.App(visible=False)
wb=app.books.open('test.xlsx')
ws= wbxl.sheets[0]
ws.shapes
xlwings docs

Related

How do I use python pandas to read an already opened excel sheet

Assuming I have an excel sheet already open, make some changes in the file and use pd.read_excel to create a dataframe based on that sheet, I understand that the dataframe will only reflect the data in the last saved version of the excel file. I would have to save the sheet first in order for pandas dataframe to take into account the change.
Is there anyway for pandas or other python packages to read an opened excel file and be able to refresh its data real time (without saving or closing the file)?
Have you tried using mitosheet package? It doesn't answer your question directly, but it allows you working on pandas dataframes as you would do in excel sheets. In this way, you may edit the data on the fly as in excel and still get a pandas dataframe as a result (meanwhile generating the code to perform the same operations with python). Does this help?
There is no way to do this. The table is not saved to disk, so pandas can not read it from disk.
Be careful not to over-engineer, that being said:
Depending on your use case, if this is really needed, I could theoretically imagine a Robotic Process Automation like e.g. BluePrism, UiPath or PowerAutomate loading live data from Excel into a Python environment with a pandas DataFrame continuously and then changing it.
This use case would have to be a really important process though, otherwise licensing RPA is not worth it here.
df = pd.read_excel("path")
In variable explorer you can see the data if you run the program in SPYDER ide

Any way to save format when importing an excel file in Python?

I'm doing some work on the data in an excel sheet using python pandas. When I write and save the data it seems that pandas only saves and cares about the raw data on the import. Meaning a lot of stuff I really want to keep such as cell colouring, font size, borders, etc get lost. Does anyone know of a way to make pandas save such things?
From what I've read so far it doesn't appear to be possible. The best solution I've found so far is to use the xlsxwriter to format the file in my code before exporting. This seems like a very tedious task that will involve a lot of testing to figure out how to achieve the various formats and aesthetic changes I need. I haven't found anything but would said writer happen to in any way be able to save the sheet format upon import?
Alternatively, what would you suggest I do to solve the problem that I have described?
Separate data from formatting. Have a sheet that contains only the data – that's the one you will be reading/writing to – and another that has formatting and reads the data from the first sheet.

Is there a way I can display data in Excel without saving a file first?

I'm using openpyxl to create a workbook in memory and fill it with data. Is there anyway to display that data in Excel at the end of the Python script without saving the file? It would be left up to the user to decide if they want to save the file or not. I'm guessing it's not possible but I wanted to see if I could get a more definitive answer here. Thanks!

How to enter values to an .xlsx file and keep formatting of cells

I have a results analysing spreadsheet where i need to enter my raw data into a 8x6 cell 'plate' which has formatted cells to produce the output based on a graph created. This .xlsx file is heavily formatted with formulas for the analysis, and it is a commercial spreadsheet so I cannot replicate these formulas.
I am using python 2.7 to obtain the raw results into a list and I have tried using xlwt and xlutils to copy the spreadsheet to enter the results. When I do this it loses all formatting when I save the file. I am wondering whether there is a different way in which I can make a copy of the spreadsheet to enter my results.
Also when I have used xlutils.copy I can only save the file as a .xls file, not an xlsx, is this the reason why it loses formatting?
First:
Apparently xlwt, does not support xlsx.
does xlwt support xlsx Format
Other library to use with format:
https://pypi.python.org/pypi/XlsxWriter
or
http://pythonexcels.com/python-excel-mini-cookbook/
While Destrif is correct, xlutils uses xlwt which doesn't support the .xlsx file format.
However, you will also find that xlsxwritter is unable to write xlrd formatted objects.
Similarly, the python-excel-cookbook he recommends only works if you are running Windows and have excel installed. A better alternative for this would be xlwings as it works for Windows and Mac with Excel installed.
If you are looking for something more platform agnostic, you could try writing your xlsx file using openpyxl.

Add sheet to created workbook from another workbook

I create new workbooks via xlsxwriter. In every of them I need to have formated header sheet, which is stored in another template workbook. I know it is impossible to do with xlsxwriter, coz I cannot open template workbook with this.
I thought to do that by xlrd, copy this sheet and then with xlsxwriter write it to created workbook.
But is it possible? To use combination of those two libraries?
I know this question is without even any code, but I'm lame with python and if you could give me any advice or something to deal with my problem I will be gratefull.
xlrd and xlswriter aren't really designed to work together. Consider switching to the pyopenxl library, which allows both reading and writing of spreadsheets and might allow you to do what you need quite easily.

Categories

Resources