Excel behaves strange with XSLX file created manually - excel

Based on knowledge gained through working with the OpenXML SDK, I have implemented an Excel generator in JS (using TypeScript with ReactJS and a custom JSX factory generating plain XML). The files generated open fine in Excel and one can also edit and save them fine in Excel, no errors.
However, if one tries to copy cells (even a single one) from such a generated Excel file to another worksheet in the same Excel instance, it fails with the error "The command cannot be used on multiple selections.". Just saving and reopening the file is enough to fix the problem. Copying to other applications (e.g. Notepad) works fine.
It seems that this particular error is shown by Excel in several edge cases where the data is not exactly meet the expected format, for instance I found reports of that happening when a sheet is hidden when manipulating it via VBA. However, in my case I'm not sure what could be causing the issue.
Just saving the file in Excel unfortunately significantly alters its parts, so that I couldn't get a meaningful diff out of it. I did not see what could be causing the problem. Maybe someone has some experience with the internals of Excel?
To get a sample file, copy the following into your browser address bar and save it as xlsx file:
data:application/vndopenxmlformats-officedocumentspreadsheetmlsheet;base64,UEsDBBQAAAgIAAAAAAA69A4d5wAAAGYBAAAPAAAAeGwvd29ya2Jvb2sueG1sjZA9T8MwEIZ3JP7DyTt1AAmhKEkXBOqCMgC7Y1+SU/0R3bktPx+3ocxM9/k+9+qa7XfwcEQWSrFV95tKAUabHMWpVZ8fr3fPCrbd7U1zSrwfUtpDEURp1ZzzUmstdsZgZJMWjGUyJg4ml5InLQujcTIj5uD1Q1U96WAoqpVQ838YaRzJ4kuyh4AxrxBGb3KxKzMtoro/Zz3rrrmck98Ikk3GVh1JaPCoIJpQyi/CE/SHwZNdQZBGeGOcEpOJoOAi3rnyDwVcU0l45x5Vwesr3+FIEd17AUrpW+Ntz3AOZ1112b0a634AUEsDBBQAAAgIAAAAAAD2SCbhNwEAAMYCAAANAAAAeGwvc3R5bGVzLnhtbJ1STWvDMAy9D/YfjO+rk8DGGEl6KAR22aUd7OokSmvwF7Zbkv36yXFK20EZ7GJJz++9KLLK9agkOYHzwuiK5quMEtCd6YXeV/Rz1zy9UrKuHx9KHyYJ2wNAICjRvqKHEOwbY747gOJ+ZSxovBmMUzxg6fbMWwe891GkJCuy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AAAAAAAAAAAAAAAAA0BAAAeGwvX3JlbHMvd29ya2Jvb2sueG1sLnJlbHNQSwECFAAUAAAICAAAAAAAY2vxKKkAAAAZAQAACwAAAAAAAAAAAAAAAAAoBQAAX3JlbHMvLnJlbHNQSwECFAAUAAAICAAAAAAAFFVBTwUBAACZAgAAEwAAAAAAAAAAAAAAAAD6BQAAW0NvbnRlbnRfVHlwZXNdLnhtbFBLBQYAAAAABgAGAIABAAAwBwAAAAA=

Well, I don't know the particulars of how you are generating the xml file, but I can tell you how to edit the underlying xml files so that it will work, and then perhaps you can figure out how to use your implementation to change the property that's gunking things up.
First, an xlsx is a set of xml files. I'm sure you know that, but I'm just starting at the beginning. You can change the extension to zip and then extract the files, and then rezip them and change the extension back to xlsx.
So do this:
take the generated xlsx
change the extension to .zip
extract the files
find xl\worksheets\sheet1.xml
open it and find this property: worksheet>sheetViews>sheetView:tabSelected
set it to 0
save the file
go back to the unzipped folder
select all files and send to zip
change the extension on the new zip file to .xlsx
You should now be able to open the newly created xlsx, add a new sheet, and copy freely.
If this works for you, then you have diagnosed the problem, one property set to true when it shouldn't be, and it should be relatively simple for you to modify your export procedure.

I've had this issue multiple times in the past.
The way I solved it was by filling out (populating) a template (file, previously created in Office) with the exported data rather than generating a file from scratch. Office unfortunately does not fully comply with OpenXML, and for more complex exports you might even be unable to open the file.
I would also recommend Beyond Compare (now Scooter Software) for comparing the two files instead of just doing a diff.

Related

Excel file getting corrupted

When I save a file with macros in office 2019 it gets corrupted. If I recover it it will remove all listobjects, shapes, etc...
If I recover it in excel 2013 it will remove just validation lists.
Is there a way to trace opening and determine what is causing the problems?
I can also upload excel file if anyone is interested to take a look at in...
Edit:
https://www.dropbox.com/s/np02i4dhkrw9trh/SQLDB%20%E2%80%93%20ZA%20KALKULACIJE.xlsm?dl=0
In this file if you add validation list at Sheet1 to "Baza Podatkov" and then save and reopen it will corrupt the file.
Corrupt Excel files is a common and known issue (when using the xlsx or xlsm format). Once a file was corrupted never trust Excel if it tells you it could be repaired. In most cases the file stays somehow corrupted (even if it tells you it was successfully repaired). Don't trust this file anymore. Start a new file, copy over the data and macros using copy/paste.
I recommend to save the files (especially if they contain big data) in the binary xlsb format. This is known to be much more staple than xlsx or xlsm and furthermore it produces much smaller files. Give it a try.
Ok I've found that I was adding items to list without using comma or something. Still weird that it didn't work.

How do I write to an existing excel file using xlwt and keep the formatting?

I am trying to change a number in an excel file (and eventually multiple excel files by putting it in a loop). I want to edit the file and save it as a new file, which I have done successfully. The problem is the new file that I save strips all of the formatting that the old excel file had. Correct me if I'm wrong: but I can't use Openpyxl because it only works for .xlsx files (all of the files I'm working with are .xls).
I've looked into pandas but was unsuccessful in finding a solution. I'm most familiar with xlrd and xlwt, but am willing to try any other libraries if it solves the problem.
import xlrd
from xlutils.copy import copy
from xlrd import *
# To open Workbook
loc = (r"X:\Projects\test.xls")
wb = xlrd.open_workbook(loc)
dose = wb.sheet_by_index(13)
manu = dose.cell_value(4,3)
#writing 675
w = copy(open_workbook(loc))
if manu == "Hologic":
w.get_sheet(13).write(5,3,675)
w.save('book2.xls')
Again, the code works without any errors. But the new .xls file has no formatting. The formatting is crucial for this project, so I can't lose any of it.
You probably can't.
Microsoft created xlsx files for a reason: the classic xls format is a legacy binary file piling up hundreds, maybe thousands, of features, each reprented in differing ways (and the file format was not even openly documented back then, I don't know if it is now). So there is one app that can open a xls file and guarrantee to present what is there with all the features intended by the file creator: Excel. And the same Excel version that created the file, in that.
So, any open library that can write to xls will create the most basic files, with no formatting - and be lucky if it can parse out the content parts.
xlsx files on the other hand use conforming xml files internally, and even a program that does not care to know about the full specs can change information in the file and preserve formatting and other things simply by not touching anything it does not know about, and assembling a valid xml again.
That said, if you can't convert to xlsx, maybe the easier thing to do is use Python to drive Excel itself to make the changes for you, in an automated way.
The documentation for that is few and far apart, but that is possible by using pywin32 and the "COM" api - take a look here for a start: https://pbpython.com/windows-com.html
Another option is using LibreOffice - it can read and write xls files with formatting (though surely with losses), and is scriptable in Python. Unfortunatelly, the information on how to script LibreOffice using Python to do that is also hard to find, and the legacy option of using their "UNO" thing to enable interaction with Python makes its use complicated.

Update linked excel path in PowerPoint via Python

I want to automate creating of a powerpoint ppt via linking template charts to some Excel files. Updating the excel file values changes the powerpoint slides automatically. I have created my powerpoint template and linked charts to sample excel files data.
I want to send the folder with the powerpoint and excel files to someone else. But this will break the link to excel files due to change in the path. (As path is not relative). I can edit the paths manually by going under the "edit links to files" option under File Menu but this is tedious as charts are numerous with multiple files.
I want to update the same via Python code using the Python-Pptx package.
Please help!
There's no API support for this in the current version of python-pptx.
You would need to modify the underlying XML directly, perhaps using python-pptx internals as a starting point and using lxml calls on the appropriate element objects. If you search on "python-pptx workaround function" you will find some examples.
Another thing to consider is modifying the XML by cruder but still possibly effective means by accessing the XML files in the .pptx package directly (the .pptx file is a Zip archive of largely XML files) and using regular expressions or perhaps a command line tool like sed or awk to do simple text substitution.
Either way you're going to need to want it pretty badly, depending on your Python skill level. You'll also of course need to discover just which strings in which parts of the XML are the ones that need changing. opc-diag can be helpful for that, but it's a bit of detective work even with the best tools.

File that normally opens our application, but will fall back to Excel

Our application exports snippets of databases in XLSX format. We wrote our own code on top of System.Packaging as it is many (many!) times faster than using the Excel objects.
Right now we save these files with a .xlsx format, and that works OK. However, it would be much nicer if double-clicking one of these opened our app instead, but failed back to Excel on machines without it.
I know that SpreadsheetML has a feature to do this. If you insert this near the top of the file:
<?mso-application progid=""Excel.Sheet""?>
some sort of magic occurs that causes Excel to open on Win machines. While this might work in SML files, it does not appear to work in "real" xlsx files - I tried adding this line to various parts of the workbook structure but it remained unrecognized.
So is there a similar mechanism we can use in "true" XLSX files generated by System.Packaging? Or some other Windows mechanism we should use in these situations?

Copy embedded text file from an Excel sheet with VBA

I have an Excel sheet with an embedded txt file which I would like to copy into a certain target folder.
I used the following code which works fine if I use it from my computer:
'copy oleobject
ActiveSheet.OLEObjects(1).Copy
'paste to activeworkbook's path
CreateObject("Shell.Application") _
.Namespace(ActiveWorkbook.path) _
.Self.InvokeVerb "Paste"
However, some of my colleagues did receive the text file which was embedded in the Excel sheet with an additional line beforehand which included the file save path in the temporary folder.
I could just delete this line with VBA but I would like to understand why this happens with other computers and not with mine.
Please help!
Thanks
OK so here is the issue:
txt file is a template and VBA is supposed to read it in again in a string file and change it. Until now it was just saved in a different folder and that worked fine but for mobility reasons (to be used by many other users) it would be a great advantage if it could be handled with just one file.
It is possible to do this with txt file embedded in Excel, but embedded files are not really well-supported by VBA automation. Most files only have a few methods available, and if I remember correctly with TXT file type, the available method is to open the file.
ActiveSheet.OLEObjects(1).OLEFormat.DoVerb 1
I mentioned a similar problem that I had (in PPT, instead of Excel, but the issue is the same). The route we chose initially was given here:
Extracting an OLEObject (XML Document) from PowerPoint VBA
Invoke the .OLEFormat.DoVerb 1 to open the file in Notepad
Use WinAPI functions to read the contents of Notepad into a string
variable
Use WinAPI functions to close Notepad
Use FileSystemObject to write/modify a new text file
Embed the new, modified text file
The functions to find and read Notepad contents are documented here and require some use of WinAPI functions (all noted in the link):
http://www.excelforum.com/excel-programming-vba-macros/729730-access-another-unsaved-excel-instance-and-unsaved-notepad-text.html
This is a LOT of work for not much benefit, instead you could simply open a file dialog prompting user to choose the text file (located on a shared network drive, etc.). This would be much more reliable.
Alternatively, depending on the size of the text file, you can store the contents inside a Shape (and you can put the shape on a hidden worksheet, etc.), either in the .TextFrame or another property that allows text. Ultimately we abandoned the solution in the links above in favor of storing the contents inside a Shape's .AlternativeText property. This worked very well for us, in storing XML contents of about 500,000 to 1 million characters per file.
The reason we chose not to go the Notepad route is that there was a lag while the file is being read and user could accidentally interrupt the procedure, corrupting the file(s), etc. and that Notepad doesn't fully support automation, etc.
I also thought of using a text field but if the oleoobject would work I thought I could also embed other file types like pictures.
Pictures are easy to work with because they have a built-in .Export method.

Resources