[Python-modules-commits] [python-odf] 04/118: Make odf2xml embed images inside the XML

Wolfgang Borgert debacle at moszumanska.debian.org
Fri Oct 3 21:27:15 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to reference refs/remotes/upstream/master
in repository python-odf.

commit 144c3e6204f8a85727678a81e2f3edb52c15cce1
Author: Søren Roug <soren.roug at eea.europa.eu>
Date:   Tue Aug 26 20:40:47 2008 +0000

    Make odf2xml embed images inside the XML
---
 odf/element.py      | 23 +++++++++++++++++++++++
 odf2xml/odf2xml     | 25 ++++++++++++++++++++++---
 odf2xml/odf2xml.1   | 23 ++++++++++++++++++-----
 odf2xml/odf2xml.xml | 32 +++++++++++++++++++++++++++-----
 4 files changed, 90 insertions(+), 13 deletions(-)

diff --git a/odf/element.py b/odf/element.py
index 7fbbc71..e6b3637 100644
--- a/odf/element.py
+++ b/odf/element.py
@@ -330,6 +330,23 @@ class Element(Node):
         else:
             self.appendChild(CDATASection(cdata))
 
+    def removeAttribute(self, attr, check_grammar=True):
+        """ Removes an attribute by name. """
+        allowed_attrs = self.allowed_attributes()
+        if allowed_attrs is None:
+            if type(attr) == type(()):
+                prefix, localname = attr
+                self.removeAttrNS(prefix, localname)
+            else:
+                raise AttributeError, "Unable to add simple attribute - use (namespace, localpart)"
+        else:
+            # Construct a list of allowed arguments
+            allowed_args = [ a[1].lower().replace('-','') for a in allowed_attrs]
+            if check_grammar and attr not in allowed_args:
+                raise AttributeError, "Attribute %s is not allowed in <%s>" % ( attr, self.tagName)
+            i = allowed_args.index(attr)
+            self.removeAttrNS(allowed_attrs[i][0], allowed_attrs[i][1])
+
     def setAttribute(self, attr, value, check_grammar=True):
         """ Add an attribute to the element
             This is sort of a convenience method. All attributes in ODF have
@@ -375,6 +392,12 @@ class Element(Node):
             self.namespaces[namespace] = prefix
         return self.attributes.get(prefix + ":" + localpart)
 
+    def removeAttrNS(self, namespace, localpart):
+        prefix = _nsassign(namespace)
+        if not self.namespaces.has_key(namespace):
+            self.namespaces[namespace] = prefix
+        del self.attributes[prefix + ":" + localpart]
+
     def getAttribute(self, attr):
         allowed_attrs = self.allowed_attributes()
         if allowed_attrs is None:
diff --git a/odf2xml/odf2xml b/odf2xml/odf2xml
index 5182f0c..fd876d6 100755
--- a/odf2xml/odf2xml
+++ b/odf2xml/odf2xml
@@ -21,16 +21,21 @@
 
 # OpenDocument can be a complete office document in a single
 # XML document. This script will create such a document.
-import sys, getopt
+import sys, getopt, base64
 from odf.opendocument import load
+from odf.draw import Image, ObjectOle
+from odf.style import BackgroundImage
+from odf.text import ListLevelStyleImage
+from odf.office import BinaryData
 
 def usage():
-   sys.stderr.write("Usage: %s [-o outputfile] [inputfile]\n" % sys.argv[0])
+   sys.stderr.write("Usage: %s [-e] [-o outputfile] [inputfile]\n" % sys.argv[0])
 
 
 if __name__ == "__main__":
+    embedimage = False
     try:
-        opts, args = getopt.getopt(sys.argv[1:], "o:", ["output="])
+        opts, args = getopt.getopt(sys.argv[1:], "o:e", ["output="])
     except getopt.GetoptError:
         usage()
         sys.exit(2)
@@ -40,6 +45,8 @@ if __name__ == "__main__":
     for o, a in opts:
         if o in ("-o", "--output"):
             outputfile = a
+        if o == '-e':
+            embedimage = True
 
     if len(args) > 1:
         usage()
@@ -48,6 +55,18 @@ if __name__ == "__main__":
         d = load(sys.stdin)
     else:
         d = load(args[0])
+    if embedimage:
+        images = d.getElementsByType(Image) + \
+           d.getElementsByType(BackgroundImage) +  \
+           d.getElementsByType(ObjectOle) + \
+           d.getElementsByType(ListLevelStyleImage)
+        for image in images:
+            href = image.getAttribute('href')
+            if href and href[:9] == "Pictures/":
+                p = d.Pictures[href]
+                bp = base64.encodestring(p[1])
+                image.addElement(BinaryData(text=bp))
+                image.removeAttribute('href')
     xml = d.xml()
     if outputfile == '-':
         sys.stdout.write(xml)
diff --git a/odf2xml/odf2xml.1 b/odf2xml/odf2xml.1
index 3d9e854..b843a8a 100644
--- a/odf2xml/odf2xml.1
+++ b/odf2xml/odf2xml.1
@@ -1,11 +1,11 @@
 .\"     Title: odf2xml
 .\"    Author: 
 .\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
-.\"      Date: 08/05/2008
+.\"      Date: 08/26/2008
 .\"    Manual: 
 .\"    Source: odfpy
 .\"
-.TH "ODF2XML" "1" "08/05/2008" "odfpy" ""
+.TH "ODF2XML" "1" "08/26/2008" "odfpy" ""
 .\" disable hyphenation
 .nh
 .\" disable justification (adjust text to left margin only)
@@ -14,12 +14,26 @@
 odf2xml - Create OpenDocument XML file from OD? package
 .SH "SYNOPSIS"
 .HP 8
-\fBodf2xml\fR [\-o\ \fIoutputfile\fR] [\fIinputfile\fR]
+\fBodf2xml\fR [\-e] [\-o\ \fIoutputfile\fR] [\fIinputfile\fR]
 .SH "DESCRIPTION"
 .PP
 OpenDocument can be a complete office document in a single XML file\. The script will take an OpenDocument and create an XML file This is mainly useful XML processors such as XSL transformation\.
 .PP
-"Inputfile" is assumed to be an OpenDocument file\. If there is no inputfile, the program will read from standard input\. If output file is not specified output will be to standard out\.
+.PP
+"Inputfile" is assumed to be an OpenDocument file\. If there is no inputfile, the program will read from standard input\.
+.SH "OPTIONS"
+.PP
+\-e
+.RS 4
+Normally, images that are stored in the archive in the Pictures folder are ignored\. Using the \-e flag will
+\fIembed\fR
+the images in the XML as base64\.
+.RE
+.PP
+\-o \fIoutputfile\fR
+.RS 4
+If output file is not specified output will be to standard out\.
+.RE
 .SH "EXAMPLE"
 .sp
 .RS 4
@@ -29,7 +43,6 @@ odf2xml \-o file\.xml  testdocument\.odt
 .RE
 .SH "SEE ALSO"
 .PP
-\fBodftools\fR(1),
 \fBxml2odf\fR(1)
 .SH "BUGS"
 .PP
diff --git a/odf2xml/odf2xml.xml b/odf2xml/odf2xml.xml
index c633130..63f6f7c 100644
--- a/odf2xml/odf2xml.xml
+++ b/odf2xml/odf2xml.xml
@@ -1,6 +1,5 @@
-<?xml version="1.0" ?>
-<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
-   "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+<?xml version="1.0"?>
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
 <refentry id="odf2xml">
   <refentryinfo>
     <productname>odfpy</productname>
@@ -16,6 +15,7 @@
   <refsynopsisdiv>
     <cmdsynopsis>
       <command>odf2xml</command>
+      <arg choice="opt">-e </arg>
       <arg choice="opt">-o <replaceable>outputfile</replaceable></arg>
       <arg choice="opt">
         <replaceable>inputfile</replaceable>
@@ -30,10 +30,33 @@ XML file. The script will take an OpenDocument and create an XML file
 This is mainly useful XML processors such as XSL transformation.
 </para>
     <para>
+</para>
+    <para>
 "Inputfile" is assumed to be an OpenDocument file.
 If there is no inputfile, the program will read from standard input.
+</para>
+  </refsect1>
+  <refsect1>
+    <title>Options</title>
+    <variablelist>
+      <varlistentry>
+        <term>-e</term>
+        <listitem>
+          <para>
+Normally, images that are stored in the archive in the Pictures folder are ignored.
+Using the -e flag will <emphasis>embed</emphasis> the images in the XML as base64.
+</para>
+        </listitem>
+      </varlistentry>
+      <varlistentry>
+        <term>-o <replaceable>outputfile</replaceable></term>
+        <listitem>
+          <para>
 If output file is not specified output will be to standard out.
 </para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
   </refsect1>
   <refsect1>
     <title>Example</title>
@@ -43,8 +66,7 @@ odf2xml -o file.xml  testdocument.odt
   </refsect1>
   <refsect1>
     <title>See Also</title>
-    <para><command>odftools</command>(1),
-          <command>xml2odf</command>(1)
+    <para><command>xml2odf</command>(1)
 </para>
   </refsect1>
   <refsect1>

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-odf.git



More information about the Python-modules-commits mailing list