I am trying to read a file with the extension .wbjn (ANSYS Workbench journal file) with basically contains python code per se, from python and change some information in it. But python does not recognize the .wbjn file, so I had to rename the .wbjn to .txt and make the changes.
But the problem with this approach is that when I trying to save the .txt extension back to .wbjn, the contents of the file is erased.
How do I do this in a better way ? Is there a library to read files with such extensions or is there another logic to solve this problem ?
Related
So for a previous question I asked how to add a custom made xmlMap to an excel file in python and I was "successful" by opening the xlsx file as an archive and extracting the file structure, followed by adding the xmlMaps.xml file to the structure and including it in the "rels". I can now open the excel file and see that the xml source map is attached, but I cannot export it. It mentions that I need to set the attribute "xmlmap.isExportable" to True, but I have no clue about how to do this, preferably using python.
All I have found on google is this: https://learn.microsoft.com/en-us/office/vba/api/excel.xmlmap.isexportable
My old question regarding the case: Adding XML Source to xlsx file in python
Any help is greatly appreciated
Best regards
Martin
Turns out I just needed to understand how a xlsx file works and how xml connections are stored and referenced throughout multiple sub files
I'm using labview to create and save data from an experiment. Labview itself creates a text file but saves it automatically with a .xls extension (word 1997-2003--it's an old setup that was never changed because it never broke). Whenever I go to open one of the data files, excel spits out this:
"The file you are trying to open, 'name.ext', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"
I'm generating a lot of data, so I want to use python to sort it out and do some quick analysis over files in a directory.
The problem is that python doesn't like that it's a text file saved with a .xls extension. It can cycle through the directory just fine to get the file names, but whenver I actually try to open the file or do anything with it, I get the error in the image attached. This happens if I change the extension to .xls, .xlsx, or do nothing with it at all and let it try to open the original filename.
error message
I literally have hundreds of these .xls files. I know I can go through, open each one in excel and save as a real excel file by hand, but that will take hours. Can someone please help me figure a way around this error in python?
Dropbox Data File set
*Update. Matlab, when trying to read one of the files using xlsread, says this:
Error using xlsread (line 251)
File C:\Users\zane\Documents\Research Projects\PneuFish Project\Data\Nov 28 2016 ATI
Data\ATI_Data_2016Y_11M_28D_16h_36m_01s.xls not in Microsoft Excel Format.
Thank you!
You can use the module xlrd.
import xlrd
import csv
def csv_from_excel():
wb = xlrd.open_workbook('your_workbook.xls')
sh = wb.sheet_by_name('Sheet1')
your_csv_file = open('your_csv_file.csv', 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in xrange(sh.nrows):
wr.writerow(sh.row_values(rownum))
your_csv_file.close()
Taken from This Post
This will convert from .xls to .csv, which is easily manipulated with Python.
You've said that the file is a text file, so don't tell Python that it's an Excel file. Just use Python's open and read it as text, then do whatever you want with it. open doesn't care what extension a file has.
I'm going to guess that the format is actually tab-delimited. From memory, earlier versions of Excel would read in tab-delimited text files with the .xls extension without complaint, whereas csv files would always bring up the text import wizard, so this was a common dodge if saving data intended for Excel from a program that didn't support writing real Excel files.
If you want the LabVIEW code to write real Excel files in future, the Write to Measurement File express VI has an option to write in xlsx format. I'm not sure which version of LabVIEW first introduced this but it's been there for a few years now.
I have a bunch of xls files to which i need to add a data validation (comboboxes) for some cells using python. I am working on linux.
Alternatively, the same could be achieved by adding a macro to a Worksheet that will run upon the file opening and insert data validation.
Most libraries I found were either for Windows or were not able to do that/edit existing file.
Any ideas what is the best (or at least possible) way to achieve that?
The project I'm working on requires that I programmatically change a .xlsx file to a .zip file, extract all of the files, then make alterations to the underlying XML that makes up an excel spreadsheet. However, I've found that whenever I attempt to write to the file that requires changes, the spreadsheet becomes corrupted once I re-zip the files and changes the extension back to .xlsx. I should note that if I manually change values within the xml files (for example, changing the name of "sheet1" to "test", there are no problems, but I need to do this using python if possible, so that isn't an option.
Does excel have some sort of checksum/integrity verifier that causes corruption when significant changes are made? If so, what does it look for and is there a way to get around it?
I'm using Windows 7 - 64 bit.
Is there a way to do this, just by relying on the file's extension?
For example: os.system(filepath) opens the given filepath using the default application, but what is the executable's filepath?
No. You can write assumptions into your program, which is what all developers do to handle these formats. It doesn't matter what extension a file has, it can be used as a format regardless.
Take for example an XML file. If you take that XML data and put it into a .txt file, or simply rename the .xml file to .txt, reading from that file and parsing the data within will still render XML formats.