PrePAN

Sign in to PrePAN

Unoconv Use LibreOffice to convert file formats

Author
charlescurley@github
Date
URL
Status
In Review
Good

Synopsis


  use Curley::Unoconv;

  ...

  $targetFile = unoconv ($srcFile, $format, $dstFile, $verbose);

  if (! defined ($targetFile)) {
    die ("Oops! Can't find source file $srcFile.\n");
  }

  if ($targetFile == -23) {
    die ("$format is not an acceptable format.\n");
  }

  if ($format eq 'csv') {
    open (SPREADSHEET, "< $targetFile") or die ("Can't open file $targetFile: $!\n");
    my @spreadsheet = <SPREADSHEET>;
    close (SPREADSHEET) or die ("Can't close file $targetFile: $!\n");

    print ($spreadsheet[0]);
  }


$verbose is optional. If it isn't provided, and there are no errors, the function is silent.

$dstFile is optional. If it isn't provided, the output file has the appropriate extension for the output format, and is in the same directory as the source file. If you provide a dummy $dstFile in order to provide a $verbose value, provide undef or ''.

The format is the file format for the resulting file, e.g. csv. The function will determine the proper extension from the format. If the format is not in the list of known formats, the function prints out a list of known formats and their associated extensions, and returns -23.

Note that the list of known formats is the formats known to unoconv, not necessarily those known to LibreOffice!

The function returns undef on other errors.

Description

Note: using my name in the module name was a stop-gap while I find a better name. Suggestions???

   Curley::Unoconv is a Perl extension that allows you to use LibreOffice (or OpenOffice.org) to convert from any spreadsheet format LibreOffice will accept to any format LibreOffice will. You can then do further processing with, e.g. Text::CSV_XS The function only does the conversion if necessary, which is determined by comparing the two files' mtimes.

   It uses Dag Wieers' unoconv, available on most Linux distributions. http://dag.wieers.com/home-made/unoconv/

   Note that the conversion can fail (and your program will die). A few notes:

   ·   The version number unoconv expects must agree with the version of LibreOffice installed. Install unoconv from the same repository you got LibreOffice from. E.g. if you install LibreOffice from debian backports, then install unoconv from debian backports.

   ·   Unoconv may not like it if the running instance of LibreOffice (if any) has a copy of the source file loaded. Save and close it.

   ·   If LibreOffice does not support the given format the whole kazoo will die messily.

EXPORT unoconv ()

SEE ALSO http://dag.wieers.com/home-made/unoconv/

   man 1 unoconv

DEFICIENCIES · There is not a lot of error checking. For example, we do not check to see if we can write to the target file.

   ·   There is no provision for passing other parameters to the unoconv program. Maybe make that an optional parameter?

   ·   How can we gracefully tell if LibreOffice supports the file format the caller wants?

   ·   We have no control over how well our back end converts. Other programs and Perl modules may do a better job for given pairs of source and destination formats.

   ·   OpenOffice::UNO might provide a more direct and flexible interface, and eliminate the need for python.

Comments

Convert::Unoconv might be a good candidate.

BTW, it's annoying that unoconv cannot be used to remove or add passwords from/to spreadsheet (both Openoffice .ods format as well as Excel format).

Please sign up to post a review.