in reply to Re: XLSX read and dump
in thread XLSX read and dump

Yes, I started with Spreadsheet::XLSX but got an error as below: IO error: opening test.xlsx for read : No such file or directory at /System/Library/Perl/Extras/5.12/Archive/Zip/Archive.pm line 546 Archive::Zip::Archive::read('Archive::Zip::Archive=HASH(0x7f87628288d8)', 'test.xlsx') called at /Library/Perl/5.12/Spreadsheet/XLSX.pm line 33 Spreadsheet::XLSX::new('Spreadsheet::XLSX', 'test.xlsx') called at parse.pl line 6 Cannot open test.xlsx as Zip archive at /Library/Perl/5.12/Spreadsheet/XLSX.pm line 33.

Replies are listed 'Best First'.
Re^3: XLSX read and dump
by talexb (Chancellor) on Jan 18, 2012 at 17:57 UTC

    First of all, a meta-comment: you should wrap your errors in code tags to a) make that part of it easier to read and b) make it stand out from the rest of your post. Ideally, the errors would look like

      IO error: opening test.xlsx for read : No such file or directory at /System/Library/Perl/Extras/5.12/Archive/ +Zip/Archive.pm line 546 Archive::Zip::Archive::read('Archive::Zip::Archive=HASH(0x7f87628288d8 +)', 'test.xlsx') called at /Library/Perl/5.12/Spreadsheet/XLSX.pm line 33 Spreadsheet::XLSX::new('Spreadsheet::XLSX', 'test.xlsx') called at parse.pl line 6 Cannot open test.xlsx as Zip archive at /Library/Perl/5.12/Spreadsheet +/XLSX.pm line 33
    I've added a few line breaks to make it easier to read.

    Second, it looks like (and I'm guessing, because you haven't posted any code yet) you are accessing the spreadsheet while it's inside a zip file. To make it easier, can you just extract one of the spreadsheets and operate on that?

    Let's try to solve one problem at a time (aka, "You've got to walk before you can run.")

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      FYI, XLSX files ARE zipped. If you look at it in an editor or hex viewer, the first two characters are PK, which indicates it's a ZIP file (from PKZIP, the forerunner of modern ZIP, where PK stands for Phil Katz, who invented it).

      If you run unzip (or pkunzip, etc.) on the file, it creates some directories that are full of XML files and RELS files (MS Office 2007+ relationship files). Any module working with an XLSX file would need to include something from the Archive::ZIP family.

      That said, I don't work on a Mac, but I would have assumed Spreadsheet::XLSX would have installed Archive::Zip::Archive as a dependency. Maybe the OP should try installing it manually.

      --marmot
        I believe the library is installed, but the file either does not exist, or is not a zip archive (i.e. not a valid xlsx file).
      The test.xlsx file isn't in a zip file. I thought it was going to create a new file called test.xlsx, but didn't. So then I tried renaming one of the files to test.xlsx and placed it in the same directory as the script below, named parse.pl, thinking that it would read it. Either way, I still get the same error about a zip archive.
      use strict; use warnings; use Spreadsheet::XLSX; my $excel = Spreadsheet::XLSX -> new ('test.xlsx'); foreach my $sheet (@{$excel -> {Worksheet}}) { printf("Sheet: %s\n", $sheet->{Sheet1}); $sheet -> {B45} ||= $sheet -> {B37}; foreach my $row ($sheet -> {B37} .. $sheet -> {B45}) { $sheet -> {R45} ||= $sheet -> {C45}; foreach my $col ($sheet -> {C45} .. $sheet -> {R45}) +{ my $cell = $sheet -> {Cells} [$row] [$col]; if ($cell) { printf("( %s , %s ) => %s\n", $row, $col, +$cell -> {Val}); } } } }
        It looks like it just can't find your test.xlsx file. Try using the full path to your file, something like:
        my $excel = Spreadsheet::XLSX -> new ('/home/chirp84/test.xlsx');
        Are you trying to parse an actual, valid, existing xlsx file, or are you trying to create one? If you are reading/parsing a spreadsheet, use Spreadsheet::XLSX. If you are trying to create one, use Excel::Writer::XLSX. Your error message indicates that either your xlsx file does not exist, or is not a valid zip archive (and xlsx files are zip archives).
          The test.xlsx file isn't in a zip file. I thought it was going to create a new file called test.xlsx, but didn't.

        Sorry -- confusion. The 'new' in this context doesn't mean it's going to *create* a file. It's creating a new context in order read one of your files that already exists.

        Next, I'm not sure

        my $excel = Spreadsheet::XLSX -> new ('test.xlsx');
        is correct. I would have expected
        my $excel = Spreadsheet::XLSX->new ('test.xlsx');
        to be better. Does your script produce any warnings when you run it?

        You need to get to the point where you're able to open a file in your current directory before we can proceed.

        Alex / talexb / Toronto

        "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds