3

I created a little script in python to generate an excel compatible xml file (saved with xls extension). The file is generated from a part database so I can place an order with the extracted data.

On the website for ordering the parts, you can import the excel file so the order fills automatically. The problem here is that each time I want to make an order, I have to open excel and save the file with xls extension of type MS Excel 97-2003 to get the import working. The excel document then looks exactly the same, but when opened with notepad, we cannot see the xml anymore, only binary dump.

Is there a way to automate this process, by running a bat file or maybe adding some line to my python script so it is converted in the proper format?

(I know that question has been asked before, but it never has been answered)

5
  • 1
    python-excel.org Commented Jan 18, 2013 at 19:10
  • 1
    Does the site accept CSV files? Commented Jan 18, 2013 at 19:15
  • @Eric Nope, it would have been easier with that. Commented Jan 18, 2013 at 19:33
  • It's worth noting that .xls is supposed to be for binary (BIFF) Excel files, not XML format, so it may just be that if you change the extension to .xml or .xslx (and/or change the content-type, depending on how you're using the file to place an order), you can use your XML-format files as-is. Commented Jan 18, 2013 at 21:16
  • 1
    Also, saying "I know that question has been asked before, but it has never been answered" would be more helpful if you gave links to previous questions (and, ideally, if they have answers that don't work for you, explain why they don't work for you). Commented Jan 18, 2013 at 21:23

1 Answer 1

5

There are two basic approaches to this.

You asked about the first: Automating Excel to open and save the file. There are in fact two ways to do that. The second is to use Python tools that can create the file directly in Python without Excel's help. So:

1a: Automating Excel through its automation interface.

Excel is designed to be controlled by external apps, through COM automation. Python has a great COM-automation interface inside of pywin32. Unfortunately, the documentation on pywin32 is not that great, and all of the documentation on Excel's COM automation interface is written for JScript, VB, .NET, or raw COM in C. Fortunately, there are a number of questions on this site about using win32com to drive Excel, such as this one, so you can probably figure it out yourself. It would look something like this:

import win32com.client
excel = win32com.client.Dispatch('Excel.Application')
spreadsheet = excel.Workbooks.Open('C:/path/to/spreadsheet.xml')
spreadsheet.SaveAs('C:/path/to/spreadsheet.xls', fileformat=excel.xlExcel8)

That isn't tested in any way, because I don't have a Windows box with Excel handy. And I vaguely remember having problems getting access to the fileformat names from win32com and just punting and looking up the equivalent numbers (a quick google for "fileformat xlExcel8" shows that the numerical equivalent is 56, and confirms that's the right format for 97-2003 binary xls).

Of course if you don't need to do it in Python, MSDN is full of great examples in JScript, VBA, etc.

The documentation you need is all on MSDN (since the Office Developer Network for Excel was merged into MSDN, and then apparently became a 404 page). The top-level page for Excel is Welcome to the Excel 2013 developer reference (if you want a different version, click on "Office client development" in the navigation thingy above and pick a different version), and what you mostly care about is the Object model reference. You can also find the same documentation (often links to the exact same webpages) in Excel's built-in help. For example, that's where you find out that the Application object has a Workbooks property, which is a Workbooks object, which has Open and Add methods that return a Workbook object, which has a SaveAs method, which takes an optional FileFormat parameter of type XlFileFormat, which has a value xlExcel8 = 56.

As I implied earlier, you may not be able to access enumeration values like xlExcel8 for some reason which I no longer remember, but you can look the value up on MSDN (or just Google it) and put the number 56 instead.

The other documentation (both here and elsewhere within MSDN) is usually either stuff you can guess yourself, or stuff that isn't relevant from win32com. Unfortunately, the already-sparse win32com documentation expects you to have read that documentation—but fortunately, the examples are enough to muddle your way through almost everything but the object model.

1b: Automating Excel via its GUI.

Automating a GUI on Windows is a huge pain, but there are a number of tools that make it a whole lot easier, such as pywinauto. You may be able to just use swapy to write the pywinauto script for you.

If you don't need to do it in Python, separate scripting systems like AutoIt have an even larger user base and even more examples to make your life easier.

2: Doing it all in Python.

xlutils, part of python-excel, may be able to do what you want, without touching Excel at all.

Sign up to request clarification or add additional context in comments.

3 Comments

I started using COM, I have a hard time finding where to get the documentation of methods and constants of excel application COM. Do you know where I should take a look?
@Speccy: I'll add some info to the answer.
Thanks. I've been struggling with this line : fileformat=excel.xlExcel8, first, fileformat should be FileFormat to remove an attribute error. Second, excel.xlExcel8 is not recognized

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.