1

I need to open an XML file and save it as a CSV. I am not interested in extracting contents (i.e. via lmtree). I just want Python to do what I would normally do (open the XML file in Excel and save as a CSV file).

I know this can work to open the XML file in Excel:

import subprocess
subprocess.Popen("%s %s" % (application_path, filepath))

...but I'm not sure where to go from here.

Thanks in advance!

Updates:

  1. Here's a sample file (it's an XML Spreadsheet file, if that helps): https://drive.google.com/open?id=193ML_2r5i56cEhydm1wYFQtXJCgRA3r_

  2. No need for Python to enable editing. I just had to read this:

    https://www.thrivenetworks.com/blog/turn-off-enable-editing-office-2010/

2

2 Answers 2

0

There's a library already built for this called xmlutils.

  1. Run pip install xmlutils to install
  2. xml2csv --input "samples/fruits.xml" --output "samples/fruits.csv" --tag "item" to convert to CSV

See this site or the documentation for more details.

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

4 Comments

Didn't work. I think the structure of the XML was is too complex.
Hmm, that's too bad. Did it give you an error? And would you mind putting down the command you used specifically?
Oh, also, this was built in Python 2.7 I believe -- are you using Python 3?
Yes, Sorry, I'll update the tags. I just substituted the XML and CSV file names with the path\names to my file and desired output. This was the error: AttributeError: 'IterParseIterator' object has no attribute 'next'
0

This worked for me:

pip install pyautogui

files=glob.glob(pathin+"*.xml")

def lazysave(inputs):
    print("%s" % (inputs))
        #subprocess.Popen("%s %s" % (path, file),shell=True)
    subprocess.Popen(inputs,shell=True)
    time.sleep(3)
    pag.FAILSAFE = True
    pag.keyDown('ctrlleft');pag.press('s');pag.keyUp('ctrlleft') #save file
    pag.PAUSE = 1
    pag.press('right') #go to "No - do not keep same format"
    #pag.PAUSE = .5
    pag.press('enter') #select "No - do not keep same format"
    #pag.PAUSE = 2
    pag.press('tab') #go to filetype dropdown menu
    #pag.PAUSE = 2
    pag.press('down') #activate filetype dropdown menu
    #pag.PAUSE = 2
    pag.press('down');pag.press('down') #go to CSV filetype
    #pag.PAUSE = .5
    pag.press('enter') #select CSV filetype
    #pag.PAUSE = .5
    pag.press('enter') #select "SAVE" button
    #pag.PAUSE = .5
    pag.press('enter') #select "Yes" button to keep CSV format
    #pag.PAUSE = .5
    pag.keyDown('altleft');pag.press('f4');pag.keyUp('alt') #close file
    pag.PAUSE = .5
    pag.press('right') #go to do not save (already saved)
    #pag.PAUSE = .5
    pag.press('enter') #select do not save (already saved)

for f in files:
     inputs=[path,f]
     lazysave(inputs)

Comments

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.