nachdem für Libreoffice 4 die Unterstützung der alten Staroffice-Formate abgekündigt wird, musste ich jetzt meine alten Dokumente endgültig konvertieren.
Also habe ich mir dafür einen perl-Hack, basierend auf unoconv http://dag.wieers.com/home-made/unoconv/ gebaut.
Über die Funktionalität von unoconv selbst hinaus, habe ich mir eingebaut:
- Bearbeitet alle Dateien in einem Verzeichnis
(Rekursiv alles Verzeichnisse darunter zu bearbeiten, ist mit einer einfachen Modifikation möglich - ich fürchtete aber von der dann umzusetzenden Zahl an Dokumenten erschlagen zu werden. - Erkennung der verschiedenen Formate (Text, Tabelle, ...)
(Das ist nicht vollständig ! Ich habe jeweils implementiert, was mir auf meinem System unterkam)
(s.u.) - Der "modified" timestamp der neuen Datei wird auf den der alten Datei gesetzt
- Hat alles geklappt, wird die alte Datei gelöscht
- .s[dx]w => .odt # writer
- .s[dx]c => .ods # calc
- .sxi => .odp # Presentation
- .svg => .odg # draw
- .sxd => .odg/# draw
Viele Grüße - und viel Erfolg beim Konvertieren...
Martin
Da ich es nicht schaffte, irgendeine Datei anzuhängen, hier also "inline".
(Und ja, Ihr müsst Euch entscheiden, ob Ihr meinem Code vertraut - aber Ihr könnt ihn Euch ja ansehen....)
Code: Alles auswählen
#!/usr/local/bin/perl -w
# $Header: /home/jost/cvs/hacks/oooConv.pl,v 1.3 2013/02/24 14:25:51 jost Exp $
use strict;
use English qw( -no_match_vars);
use IO::File;
use File::Find;
use Getopt::Long;
use constant UNOCONV => "/home/jost/transfer/unoconv-0.6/unoconv";
my($matched, # Files matching filter
$tried, # Tried to transform
$succeeded); # Succeeded to transform
$matched = 0;
$tried = 0;
$succeeded = 0;
sub manual()
{
print STDERR <<EOM;
Converts old OOO (SO) files to new ones; sets timestamp and removes the old relicts
Old is'*.sdw', new '*.odt'.
The timestamp of the new file is set from the old one, then the old fiel is removed.
Uses unoconv (http://dag.wieers.com/home-made/unoconv/) for the actual conversion
call: oooConv DIR
EOM
}
sub wanted
{
# Called for each element found below given dir
# $File::Find::name: name of the found object
my($elem) = $File::Find::name;
if (!-f $elem)
{
return; # No file
}
if (($elem !~ /\.sd[cw]$/i) &&
($elem !~ /\.sx[cdiw]$/i) &&
($elem !~ /\.svg$/i))
{
return; # Wrong extension
}
print "=== $elem ===\n";
$matched++;
my($new,
$format);
$new = $elem;
if ($elem =~ /\.s[dx]w$/i)
{
$new =~ s/\.s[dx]w$/.odt/i; # writer
$format = "odt";
}
elsif ($elem =~ /\.s[dx]c$/i)
{
$new =~ s/\.s[dx]c$/.ods/i; # calc
$format = "ods";
}
elsif ($elem =~ /\.sxi$/i)
{
$new =~ s/\.sxi$/.odp/i; # Presentation
$format = "odp";
}
elsif (($elem =~ /\.svg$/i) ||
($elem =~ /\.sxd$/i))
{
$new =~ s/\.svg$/.odg/i; # draw
$new =~ s/\.sxd$/.odg/i; # draw
$format = "odg";
}
if (-e $new)
{
print STDERR " Error: \"$new\" already exists !\n";
return;
}
$tried++;
my($cmd,
$ret);
$cmd = UNOCONV . " -v -f $format \"$elem\"";
$ret = system($cmd);
if ($ret == -1)
{
print STDERR " Error: Failed to execute: $ERRNO\n";
return;
}
elsif ($ret & 127)
{
printf STDERR " Error: Died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
return;
}
elsif ($ret != 0)
{
printf STDERR " Error: Exited with value %d\n", $? >> 8;
return;
}
my($mtimeOld,
$mtimeNew);
$mtimeOld = (stat($elem))[9];
if (!-f $new)
{
printf STDERR " Error: '.odt-file doesn't exist !\n";
return;
}
$mtimeNew = (stat($new))[9];
if ($mtimeOld > $mtimeNew)
{
printf STDERR " Error: New file is older than old one !\n";
return;
}
my($now);
$now = time;
$ret = utime($now, $mtimeOld, $new);
if ($ret != 1)
{
printf STDERR " Error: umtime failed !\n";
return;
}
$ret = unlink($elem);
if (!$ret)
{
printf STDERR " Error: unlink failed: $ERRNO !\n";
return;
}
$succeeded++;
}
if ($#ARGV < 0)
{
print STDERR "Error: No argument (dir) given ! \n";
manual();
exit 1;
}
my($dir) = $ARGV[0];
if (! -d $dir)
{
print STDERR "Error: Expected Dir as argument ! \n";
manual();
exit 1;
}
find(\&wanted, $dir);
if ($matched > 0)
{
print "\n==================================\n";
print "$matched files passed filter\n";
print "$tried files tried to transform\n";
if ($tried - $succeeded > 0)
{
print "" . ($tried - $succeeded) . " files FAILED\n";
}
else
{
print "ALL files SUCCESSFULL\n";
}
}