0

I found a command online that works for unzipping docx files but I haven't been able to tweak it to unzip xlsx files. The command is

unzip -p some.docx word/document.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g'

and I'm having trouble understanding the 2nd parameter word/document.xml, and how it's used.

When I tried converting an xlsx file, I tried:

unzip -p testexcel.xlsx excel/spreadsheet.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g'

but I have no idea what to put in that second parameter in place of excel/spreadsheet.xml.

3
  • 2
    Have you looked at the output of unzip -t some.docx? – steeldriver Nov 1 '17 at 0:27
  • 1
    You realize that's likely an Excel spreadsheet? What's your end-goal here? – Jeff Schaller Nov 1 '17 at 0:52
  • End goal is to get all the text in the file. There are multiple spreadsheets as well. @steeldriver does that option list the available parameters? Looking at the man page, but it doesn't seem to help explain what it lists – simplycoding Nov 1 '17 at 16:37
4

The parameter word/document.xml is the path of the file within the zip archive you are telling unzip to return. This is part of the standard structure of filetypes like docx.

For Excel xlsx files, you probably want xl/worksheets/sheet1.xml. If there are further worksheets, they are sheet2.xml, etc. This is regardless of the names you give the worksheets, which are stored in xl/workbook.xml along with general spreadsheet info.

If you're unsure of the zip file structure, you can use the -l option to list the files it contains.

unzip -l some.docx

Omitting the option just extracts the file to the current directory. The -p used in the command you're using pipes the output to stdout so sed can process it.

1

You posted this command:

unzip -p some.docx word/document.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g'

First, to clarify what it does:

  • it extracts a file called word/document.xml from the .docx file
  • this it piped to the sed command which somehow "beautifies" the output by removing all formatting information

So, to find out how you can do this with an .xlsx, try unzip foo.xlsx. You can see now that the file you are interested in may be xl/worksheets/sheet1.xml.

But if you want to extract the content of an .xlsx I strongly recommend not to use this way.

There are several tools to convert an .xlsx to CSV, which basically contains the content of the table sepperated by commas.

This question has multiple answers how to convert form .xlsx to CSV.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.