Let me try to improve this question, as it is still valid for me.
I have been using Openpyxl to read Excel files for a while. Now I need to extend the capability of my script to handle "legacy" Excel files that are not supported by Openpyxl. For this I use xlrd and xlutils.
On issue I have yet to solve is how to get the modified date of an .xls file in the case where I don't have the path. Using Openpyxl, I can get this as Workbook.properties.modified, as a datetime object. With xlrd I am struggling. A workaround would be to figure out the path to the file (which may be input to my function as a file like object) and to use os.path.getmtime, but I am not sure if that is equivalent.
Any help is appreciated!
I just came across your post while searching for a solution myself. Seems like
wb = xlrd.open_workbook(filename=fn)
wb.props.get('modified')
does the job.
xlrd==1.2.0
Related
I'm working on an program which makes an excel file, then it gets the info into JSON and does more things. I'm struggling with Openpyxl. I found out today that if you don't open an Excel file made with Openpyxl with Excel, the formulas won't be computer.
So when I write:
excel = load_workbook(self.path_excel, read_only=True, data_only=True)
I don't get the formulas result, but only a "None" result. If I instead write data_only=False I will get my original formula. I very well know why this happens and I'm trying to find an automatic solution to open the excel file, compute all the formulas inside my excel file and close it. So when I open it up again in Openpyxl in the code after this "phase" I will have my results.
I'm using Python btw.
Here is the result I get and what I want to get:
1: data_only=True
data_only=False
What I really want with data_only=True
'delta_1': '12345' and more answers with numbers like when I open it in excel...
Thanks for the eventual help :)
I'm answering my own post for future reference and for others having my same problem...
Basically what worked was:
Stop using Pycharm as for some reason it was limiting my Python code;
Download Visual Studio Code;
Use the xlwings library as suggested by jezza_99 in the second comment.
Thanks for the help everyone :)
I am trying to read in an 'xls' files in python using pandas. My code basically is a one-liner:
import pandas as pd
df = pd.read_excel(str("/test/test_file.xls"))
This code works for the majority of the files, but there are cases when it fails with the error:
Excessive indirect references in NAME formula
What I tried so far:
Tried changing the stack limit(panic and warning) to as far as 10000 in the Pandas package itself, where the exception was occurring. A recursion limit was encountered, so raised it as far as 125000, which led to my Mac/Python reaching its limit so I am guessing not the right solution.
Used a memory-intensive EMR to see if it can read the file - nope.
Looked at the GitHub repo for XLRD here to raise a bug only to find out it's out of support.
Opened the file, saved it as an xlsx, used the same code to read it into a dataframe. Worked like a charm.
Tried using Spark Excel Library to read in a particular section of the data - this worked too but I need to use pandas.
Googled it only to find out the results would show me the XLRD code where the exception is defined. Not one person has reported it.
Tried using Python2 and Python3 with the latest and older versions of Pandas - no use.
I cannot share the file, but has anyone faced this issue before? Can someone help? All suggestions are welcome!
Try the following:
Open the xls file
Copy/paste all cells as values
Rerun your script
Hard to help further without having access to the file to explain exactly what is happening.
But chances are xlrd is trying to resolve the value of a formula and is exceeding the "STACK_PANIC_LEVEL". Without seeing the formula, very difficult to say more.
xlrd has a method of evaluate_name_formula(). When you try to open a .xls file with xlrd, it will raise an error (as you described) if your file has many user-defined formulas. To try to solve your problem, I think you can delete these user-defined formulas and keep the file free of these formulas. Or you can try to edit xlrd code, and prevent it from raising the Error, which seems much more difficult.
I'm trying to make a file that looks like excel with few sheets.
The goal is to be able to read from python 3.6 a specific sheet---> then to read specific column from this shit.
for example:
I want to read from python the dates sheet the time column.
If there is an option to do so with csv/any better file i would really like to hear or if there is a way to read like this from excel it would also work.
Thank you.
You can try openpyxl module if xlrd is not working (that is an interesting case). It is very simple to use. I didn't try but it should work on Python 3.6.
You can find some sample codes here.
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.
Is there a way to write something into an XLS file with python while keeping the initial format of this XLS file unchanged (such as font size, cell background color, etc)Thanks!
Maybe have a look at xlrd, xlwt, xlutils:
http://www.python-excel.org/
I have found it pretty useful in the past.
If you can go for XML serializations of Office documents. Also have a look at ... http://www.python-excel.org/
You could also use pywin32 if you are using Windows
If the available tools like xlrd, xlwt, and xlutils don't work, you may have to fall back to programmatically editing the file with Excel via COM. Of course, you'll need a copy of Excel, have to work from Windows, and it will be slower than other approaches.