From 72fe485b2e7d46ad60cf6fd8b4ceb645c6552ccc Mon Sep 17 00:00:00 2001 From: Jerome Dumonteil Date: Thu, 26 Oct 2023 22:09:45 +0200 Subject: [PATCH] fix doc --- doc/scripts/diff.html | 196 ++++++++++++++++ doc/scripts/folder.html | 152 +++++++++++++ doc/scripts/index.html | 80 +++++++ doc/scripts/show.html | 465 ++++++++++++++++++++++++++++++++++++++ doc/scripts/styles.html | 468 +++++++++++++++++++++++++++++++++++++++ doc/templates/index.html | 53 +++++ 6 files changed, 1414 insertions(+) create mode 100644 doc/scripts/diff.html create mode 100644 doc/scripts/folder.html create mode 100644 doc/scripts/index.html create mode 100644 doc/scripts/show.html create mode 100644 doc/scripts/styles.html create mode 100644 doc/templates/index.html diff --git a/doc/scripts/diff.html b/doc/scripts/diff.html new file mode 100644 index 0000000..8df5c5c --- /dev/null +++ b/doc/scripts/diff.html @@ -0,0 +1,196 @@ + + + + + + +odfdo.scripts.diff API documentation + + + + + + + + + + + +
+
+
+

Module odfdo.scripts.diff

+
+
+
+ +Expand source code + +
#!/usr/bin/env python
+# Copyright 2018-2023 Jérôme Dumonteil
+# Copyright (c) 2009-2013 Ars Aperta, Itaapy, Pierlis, Talend.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+# Authors (odfdo project): jerome.dumonteil@gmail.com
+# The odfdo project is a derivative work of the lpod-python project:
+# https://github.com/lpod/lpod-python
+# Authors: David Versmisse <david.versmisse@itaapy.com>
+
+import sys
+from difflib import ndiff, unified_diff
+from optparse import OptionParser
+from os import stat
+from time import ctime
+
+from odfdo import Document, __version__
+
+
+def main():
+    usage = "%prog <doc1.odt> <doc2.odt>"
+    description = "Show a diff between doc1.odt and doc2.odt"
+    parser = OptionParser(usage, version=__version__, description=description)
+
+    # --ndiff
+    parser.add_option(
+        "-n",
+        "--ndiff",
+        action="store_true",
+        default=False,
+        help='use a contextual "ndiff" format to show the output',
+    )
+
+    # Parse !
+    options, args = parser.parse_args()
+
+    # Go !
+    if len(args) != 2:
+        parser.print_help()
+        sys.exit(1)
+
+    # Open the 2 documents, diff only for ODT
+    doc1 = Document(args[0])
+    doc2 = Document(args[1])
+    if doc1.get_type() != "text" or doc2.get_type() != "text":
+        parser.print_help()
+        sys.exit(1)
+
+    # Convert in text before the diff
+    text1 = doc1.get_formatted_text(True).splitlines(True)
+    text2 = doc2.get_formatted_text(True).splitlines(True)
+
+    # Make the diff !
+    if options.ndiff:
+        result = ndiff(text1, text2, None, None)
+        result = [line for line in result if not line.startswith(" ")]
+    else:
+        fromdate = ctime(stat(args[0]).st_mtime)
+        todate = ctime(stat(args[1]).st_mtime)
+        result = unified_diff(text1, text2, args[0], args[1], fromdate, todate)
+    print("".join(result))
+
+
+if __name__ == "__main__":
+    main()
+
+
+
+
+
+
+
+

Functions

+
+
+def main() +
+
+
+
+ +Expand source code + +
def main():
+    usage = "%prog <doc1.odt> <doc2.odt>"
+    description = "Show a diff between doc1.odt and doc2.odt"
+    parser = OptionParser(usage, version=__version__, description=description)
+
+    # --ndiff
+    parser.add_option(
+        "-n",
+        "--ndiff",
+        action="store_true",
+        default=False,
+        help='use a contextual "ndiff" format to show the output',
+    )
+
+    # Parse !
+    options, args = parser.parse_args()
+
+    # Go !
+    if len(args) != 2:
+        parser.print_help()
+        sys.exit(1)
+
+    # Open the 2 documents, diff only for ODT
+    doc1 = Document(args[0])
+    doc2 = Document(args[1])
+    if doc1.get_type() != "text" or doc2.get_type() != "text":
+        parser.print_help()
+        sys.exit(1)
+
+    # Convert in text before the diff
+    text1 = doc1.get_formatted_text(True).splitlines(True)
+    text2 = doc2.get_formatted_text(True).splitlines(True)
+
+    # Make the diff !
+    if options.ndiff:
+        result = ndiff(text1, text2, None, None)
+        result = [line for line in result if not line.startswith(" ")]
+    else:
+        fromdate = ctime(stat(args[0]).st_mtime)
+        todate = ctime(stat(args[1]).st_mtime)
+        result = unified_diff(text1, text2, args[0], args[1], fromdate, todate)
+    print("".join(result))
+
+
+
+
+
+
+
+ +
+ + + \ No newline at end of file diff --git a/doc/scripts/folder.html b/doc/scripts/folder.html new file mode 100644 index 0000000..cec48ed --- /dev/null +++ b/doc/scripts/folder.html @@ -0,0 +1,152 @@ + + + + + + +odfdo.scripts.folder API documentation + + + + + + + + + + + +
+
+
+

Module odfdo.scripts.folder

+
+
+
+ +Expand source code + +
#!/usr/bin/env python
+# Copyright 2018-2023 Jérôme Dumonteil
+# Copyright (c) 2009-2013 Ars Aperta, Itaapy, Pierlis, Talend.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+# Authors (odfdo project): jerome.dumonteil@gmail.com
+# The odfdo project is a derivative work of the lpod-python project:
+# https://github.com/lpod/lpod-python
+# Authors: Jerome Dumonteil <jerome.dumonteil@itaapy.com>
+
+import os
+import sys
+from optparse import OptionParser
+
+from odfdo import Document, __version__
+
+
+def main():
+    usage = "%prog <input>"
+    description = "Convert standard ODF File to folder, and reverse."
+    parser = OptionParser(usage, version=__version__, description=description)
+
+    # Parse !
+    options, args = parser.parse_args()
+
+    # Go !
+    if len(args) != 1:
+        parser.print_help()
+        sys.exit(0)
+
+    if os.path.isfile(args[0]):
+        out_packaging = "folder"
+    elif os.path.isdir(args[0]):
+        out_packaging = "zip"
+    else:
+        raise ValueError("no File or folder ?")
+    doc = Document(args[0])
+    doc.save(packaging=out_packaging, pretty=True)
+
+
+if __name__ == "__main__":
+    main()
+
+
+
+
+
+
+
+

Functions

+
+
+def main() +
+
+
+
+ +Expand source code + +
def main():
+    usage = "%prog <input>"
+    description = "Convert standard ODF File to folder, and reverse."
+    parser = OptionParser(usage, version=__version__, description=description)
+
+    # Parse !
+    options, args = parser.parse_args()
+
+    # Go !
+    if len(args) != 1:
+        parser.print_help()
+        sys.exit(0)
+
+    if os.path.isfile(args[0]):
+        out_packaging = "folder"
+    elif os.path.isdir(args[0]):
+        out_packaging = "zip"
+    else:
+        raise ValueError("no File or folder ?")
+    doc = Document(args[0])
+    doc.save(packaging=out_packaging, pretty=True)
+
+
+
+
+
+
+
+ +
+ + + \ No newline at end of file diff --git a/doc/scripts/index.html b/doc/scripts/index.html new file mode 100644 index 0000000..a6684bc --- /dev/null +++ b/doc/scripts/index.html @@ -0,0 +1,80 @@ + + + + + + +odfdo.scripts API documentation + + + + + + + + + + + +
+ + +
+ + + \ No newline at end of file diff --git a/doc/scripts/show.html b/doc/scripts/show.html new file mode 100644 index 0000000..e8b278a --- /dev/null +++ b/doc/scripts/show.html @@ -0,0 +1,465 @@ + + + + + + +odfdo.scripts.show API documentation + + + + + + + + + + + +
+
+
+

Module odfdo.scripts.show

+
+
+
+ +Expand source code + +
#!/usr/bin/env python
+# Copyright 2018-2023 Jérôme Dumonteil
+# Copyright (c) 2009-2010 Ars Aperta, Itaapy, Pierlis, Talend.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+# Authors (odfdo project): jerome.dumonteil@gmail.com
+# The odfdo project is a derivative work of the lpod-python project:
+# https://github.com/lpod/lpod-python
+# Authors: Hervé Cauwelier <herve@itaapy.com>
+#          David Versmisse <david.versmisse@itaapy.com>
+
+import sys
+from optparse import OptionParser
+from os import makedirs, mkdir
+from os.path import exists, join
+from shutil import rmtree
+
+# from odfdo.cleaner import test_document
+from odfdo import Document, __version__
+from odfdo.scriptutils import add_option_output, check_target_directory, printerr
+
+
+def clean_filename(filename):
+    allowed_characters = {".", "-", "_", "@"}
+    result = []
+    for c in filename:
+        if c not in allowed_characters and not c.isalnum():
+            result.append("_")
+        else:
+            result.append(c)
+    return "".join(result)
+
+
+def dump_pictures(document, target):
+    for part_name in document.get_parts():
+        if part_name.startswith("Pictures/"):
+            path = join(target, "Pictures")
+            if not exists(path):
+                mkdir(path)
+            data = document.get_part(part_name)
+            path = join(target, part_name)
+            with open(path, "wb") as f:
+                f.write(data)
+
+
+def spreadsheet_to_stdout(document):
+    body = document.body
+    for table in body.get_tables():
+        table.rstrip(aggressive=True)
+        print(table.to_csv(None))
+
+
+def spreadsheet_to_csv(document):
+    body = document.get_body()
+    for table in body.get_tables():
+        name = table.get_name()
+        filename = clean_filename(name) + ".csv"
+        table.rstrip(aggressive=True)
+        table.to_csv(filename)
+
+
+def show_output(container_url, options, doc, doc_type):
+    output = options.output
+    check_target_directory(output)
+    if exists(output):
+        rmtree(output)
+    makedirs(output)
+    with open(join(output, "meta.txt"), "w") as f:
+        f.write(doc.get_formated_meta())
+    with open(join(output, "styles.txt"), "w") as f:
+        f.write(doc.show_styles())
+    dump_pictures(doc, output)
+
+    if doc_type in {"text", "text-template", "presentation", "presentation-template"}:
+        with open(join(output, "content.rst"), "w") as f:
+            f.write(doc.get_formatted_text(rst_mode=options.rst))
+    # spreadsheet
+    elif doc_type in {"spreadsheet", "spreadsheet-template"}:
+        spreadsheet_to_csv(doc)
+    else:
+        printerr("The OpenDocument format", doc_type, "is not supported yet.")
+        sys.exit(1)
+
+
+def show(container_url, options):
+    # Open it!
+    doc = Document(container_url)
+    doc_type = doc.get_type()
+    # Test it! XXX for TEXT only
+    # if doc_type == 'text':
+    #    result = test_document(document)
+    #    if result is not True:
+    #        print('This file is malformed: %s' % result)
+    #        print('Please use lpod-clean.py to fix it')
+    #        exit(1)
+    if options.output:
+        return show_output(container_url, options, doc, doc_type)
+    if options.meta:
+        print(doc.get_formated_meta())
+    if options.styles:
+        print(doc.show_styles())
+    if (
+        doc_type in {"text", "text-template", "presentation", "presentation-template"}
+        and not options.no_content
+    ):
+        print(doc.get_formatted_text(rst_mode=options.rst))
+    elif doc_type in {"spreadsheet", "spreadsheet-template"} and not options.no_content:
+        spreadsheet_to_stdout(doc)
+    else:
+        printerr("The OpenDocument format", doc_type, "is not supported yet.")
+        sys.exit(1)
+
+
+def main():
+    # Options initialisation
+    usage = (
+        "%prog [--styles] [--meta] [--no-content] [--rst] <file>\n"
+        "       %prog -o <DIR> [--rst] <file>"
+    )
+    description = (
+        "Dump text from an OpenDocument file to the standard "
+        "output, optionally styles and meta (and the Pictures/* "
+        'in "-o <DIR>" mode)'
+    )
+    parser = OptionParser(usage, version=__version__, description=description)
+    # --meta
+    parser.add_option(
+        "-m",
+        "--meta",
+        action="store_true",
+        default=False,
+        help="dump metadata to stdout",
+    )
+    # --styles
+    parser.add_option(
+        "-s",
+        "--styles",
+        action="store_true",
+        default=False,
+        help="dump styles to stdout",
+    )
+    # --no-content
+    parser.add_option(
+        "-n",
+        "--no-content",
+        action="store_true",
+        default=False,
+        help="do not dump content to stdout",
+    )
+    # --rst
+    parser.add_option(
+        "-r",
+        "--rst",
+        action="store_true",
+        default=False,
+        help="Dump the content file with a reST syntax",
+    )
+    # --output
+    add_option_output(parser, metavar="DIR")
+    # Parse !
+    options, args = parser.parse_args()
+    # Container
+    if len(args) != 1:
+        parser.print_help()
+        exit(1)
+    container_url = args[0]
+    show(container_url, options)
+
+
+if __name__ == "__main__":
+    main()
+
+
+
+
+
+
+
+

Functions

+
+
+def clean_filename(filename) +
+
+
+
+ +Expand source code + +
def clean_filename(filename):
+    allowed_characters = {".", "-", "_", "@"}
+    result = []
+    for c in filename:
+        if c not in allowed_characters and not c.isalnum():
+            result.append("_")
+        else:
+            result.append(c)
+    return "".join(result)
+
+
+
+def dump_pictures(document, target) +
+
+
+
+ +Expand source code + +
def dump_pictures(document, target):
+    for part_name in document.get_parts():
+        if part_name.startswith("Pictures/"):
+            path = join(target, "Pictures")
+            if not exists(path):
+                mkdir(path)
+            data = document.get_part(part_name)
+            path = join(target, part_name)
+            with open(path, "wb") as f:
+                f.write(data)
+
+
+
+def main() +
+
+
+
+ +Expand source code + +
def main():
+    # Options initialisation
+    usage = (
+        "%prog [--styles] [--meta] [--no-content] [--rst] <file>\n"
+        "       %prog -o <DIR> [--rst] <file>"
+    )
+    description = (
+        "Dump text from an OpenDocument file to the standard "
+        "output, optionally styles and meta (and the Pictures/* "
+        'in "-o <DIR>" mode)'
+    )
+    parser = OptionParser(usage, version=__version__, description=description)
+    # --meta
+    parser.add_option(
+        "-m",
+        "--meta",
+        action="store_true",
+        default=False,
+        help="dump metadata to stdout",
+    )
+    # --styles
+    parser.add_option(
+        "-s",
+        "--styles",
+        action="store_true",
+        default=False,
+        help="dump styles to stdout",
+    )
+    # --no-content
+    parser.add_option(
+        "-n",
+        "--no-content",
+        action="store_true",
+        default=False,
+        help="do not dump content to stdout",
+    )
+    # --rst
+    parser.add_option(
+        "-r",
+        "--rst",
+        action="store_true",
+        default=False,
+        help="Dump the content file with a reST syntax",
+    )
+    # --output
+    add_option_output(parser, metavar="DIR")
+    # Parse !
+    options, args = parser.parse_args()
+    # Container
+    if len(args) != 1:
+        parser.print_help()
+        exit(1)
+    container_url = args[0]
+    show(container_url, options)
+
+
+
+def show(container_url, options) +
+
+
+
+ +Expand source code + +
def show(container_url, options):
+    # Open it!
+    doc = Document(container_url)
+    doc_type = doc.get_type()
+    # Test it! XXX for TEXT only
+    # if doc_type == 'text':
+    #    result = test_document(document)
+    #    if result is not True:
+    #        print('This file is malformed: %s' % result)
+    #        print('Please use lpod-clean.py to fix it')
+    #        exit(1)
+    if options.output:
+        return show_output(container_url, options, doc, doc_type)
+    if options.meta:
+        print(doc.get_formated_meta())
+    if options.styles:
+        print(doc.show_styles())
+    if (
+        doc_type in {"text", "text-template", "presentation", "presentation-template"}
+        and not options.no_content
+    ):
+        print(doc.get_formatted_text(rst_mode=options.rst))
+    elif doc_type in {"spreadsheet", "spreadsheet-template"} and not options.no_content:
+        spreadsheet_to_stdout(doc)
+    else:
+        printerr("The OpenDocument format", doc_type, "is not supported yet.")
+        sys.exit(1)
+
+
+
+def show_output(container_url, options, doc, doc_type) +
+
+
+
+ +Expand source code + +
def show_output(container_url, options, doc, doc_type):
+    output = options.output
+    check_target_directory(output)
+    if exists(output):
+        rmtree(output)
+    makedirs(output)
+    with open(join(output, "meta.txt"), "w") as f:
+        f.write(doc.get_formated_meta())
+    with open(join(output, "styles.txt"), "w") as f:
+        f.write(doc.show_styles())
+    dump_pictures(doc, output)
+
+    if doc_type in {"text", "text-template", "presentation", "presentation-template"}:
+        with open(join(output, "content.rst"), "w") as f:
+            f.write(doc.get_formatted_text(rst_mode=options.rst))
+    # spreadsheet
+    elif doc_type in {"spreadsheet", "spreadsheet-template"}:
+        spreadsheet_to_csv(doc)
+    else:
+        printerr("The OpenDocument format", doc_type, "is not supported yet.")
+        sys.exit(1)
+
+
+
+def spreadsheet_to_csv(document) +
+
+
+
+ +Expand source code + +
def spreadsheet_to_csv(document):
+    body = document.get_body()
+    for table in body.get_tables():
+        name = table.get_name()
+        filename = clean_filename(name) + ".csv"
+        table.rstrip(aggressive=True)
+        table.to_csv(filename)
+
+
+
+def spreadsheet_to_stdout(document) +
+
+
+
+ +Expand source code + +
def spreadsheet_to_stdout(document):
+    body = document.body
+    for table in body.get_tables():
+        table.rstrip(aggressive=True)
+        print(table.to_csv(None))
+
+
+
+
+
+
+
+ +
+ + + \ No newline at end of file diff --git a/doc/scripts/styles.html b/doc/scripts/styles.html new file mode 100644 index 0000000..771ff87 --- /dev/null +++ b/doc/scripts/styles.html @@ -0,0 +1,468 @@ + + + + + + +odfdo.scripts.styles API documentation + + + + + + + + + + + +
+
+
+

Module odfdo.scripts.styles

+
+
+
+ +Expand source code + +
#!/usr/bin/env python
+# Copyright 2018-2023 Jérôme Dumonteil
+# Copyright (c) 2009-2010 Ars Aperta, Itaapy, Pierlis, Talend.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+# Authors (odfdo project): jerome.dumonteil@gmail.com
+# The odfdo project is a derivative work of the lpod-python project:
+# https://github.com/lpod/lpod-python
+# Authors: Hervé Cauwelier <herve@itaapy.com>
+#          Romain Gauthier <romain@itaapy.com>
+
+import sys
+from optparse import OptionParser
+
+from odfdo import Document, __version__
+from odfdo.const import ODF_CLASSES
+from odfdo.scriptutils import (
+    StdoutWriter,
+    add_option_output,
+    check_target_file,
+    printerr,
+    printinfo,
+)
+
+
+def show_styles(document, target=None, automatic=True, common=True, properties=False):
+    """Show the different styles of a document and their properties."""
+    output = document.show_styles(
+        automatic=automatic, common=common, properties=properties
+    )
+    # Print the output
+    if target is None:
+        print(output, end="")
+        return
+    with open(target, "w") as f:
+        f.write(output)
+
+
+def delete_styles(document, target, pretty=True):
+    n = document.delete_styles()
+    document.save(target=target, pretty=pretty)
+    printinfo(n, "styles removed (0 error, 0 warning).")
+
+
+def find_presentation_list_style(body):
+    for frame in body.get_frames(presentation_class="outline"):
+        first_list = frame.get_list()
+        if first_list is not None:
+            return first_list.get_style()
+    return None
+
+
+def merge_presentation_styles(document, source):
+    # Apply master page found
+    source_body = source.body
+    first_page = source_body.get_draw_page()
+    print(first_page.serialize())
+    master_page_name = first_page.master_page
+    print(master_page_name)
+    first_master_page = document.get_style("master-page", master_page_name)
+    printinfo("master page used:", first_master_page.display_name)
+    body = document.get_body
+    for page in body.get_draw_pages():
+        page.set_style_attribute(first_page.get_style())
+        page.set_master_page(first_page.get_master_page())
+        page.set_presentation_page_layout(first_page.get_presentation_page_layout())
+    # Adjust layout -- will obviously work only if content is separated from
+    # style: use of master pages, layout, etc.
+    for presentation_class in ODF_CLASSES:
+        first_frame = source_body.get_frame(presentation_class=presentation_class)
+        if first_frame is None:
+            continue
+        # Mimic frame style
+        position = first_frame.get_position()
+        size = first_frame.size
+        style = first_frame.style
+        presentation_style = first_frame.get_presentation_style()
+        for page in body.get_draw_pages():
+            for frame in page.get_frames(presentation_class=presentation_class):
+                frame.position = position
+                frame.size = size
+                frame.set_style_attribute(style)
+                frame.set_presentation_style(presentation_style)
+        # Mimic list style (XXX only first level)
+        if presentation_class == "outline":
+            list_style = find_presentation_list_style(source_body)
+            for page in body.get_draw_pages():
+                for frame in page.get_frames(presentation_class="outline"):
+                    for lst in frame.get_lists():
+                        lst.set_style_attribute(list_style)
+
+
+def merge_styles(document, from_file, target=None, pretty=True):
+    source = Document(from_file)
+    document.delete_styles()
+    document.merge_styles_from(source)
+    doc_type = document.get_type()
+    # Enhance Presentation merge
+    if doc_type in {"presentation", "presentation-template"}:
+        printinfo("merging presentation styles...")
+        merge_presentation_styles(document, source)
+    document.save(target=target, pretty=pretty)
+    printinfo("Done (0 error, 0 warning).")
+
+
+def main():
+    # Options initialisation
+    usage = "%prog [options] <file>"
+    description = "A command line interface to manipulate styles of OpenDocument files."
+    parser = OptionParser(usage, version=__version__, description=description)
+    # --automatic
+    parser.add_option(
+        "-a",
+        "--automatic",
+        action="store_true",
+        default=False,
+        help="show automatic styles only",
+    )
+    # --common
+    parser.add_option(
+        "-c",
+        "--common",
+        action="store_true",
+        default=False,
+        help="show common styles only",
+    )
+    # --properties
+    parser.add_option(
+        "-p", "--properties", action="store_true", help="show properties of styles"
+    )
+    # --delete
+    msg = "return a copy with all styles (except default) deleted from <file>"
+    parser.add_option("-d", "--delete", action="store_true", help=msg)
+    # --merge
+    msg = (
+        "copy styles from FILE to <file>. Any style with the same "
+        "name will be replaced."
+    )
+    parser.add_option(
+        "-m", "--merge-styles-from", dest="merge", metavar="FILE", help=msg
+    )
+    # --output
+    add_option_output(parser)
+    # Parse options
+    options, args = parser.parse_args()
+    if len(args) != 1:
+        parser.print_help()
+        sys.exit(1)
+    doc = Document(args[0])
+    if options.delete:
+        target = options.output
+        if target is None:
+            printerr(
+                "Will not delete in-place: ", 'output file needed or "-" for stdout'
+            )
+            sys.exit(1)
+        elif target == "-":
+            target = StdoutWriter()
+        else:
+            check_target_file(target)
+        delete_styles(doc, target)
+    elif options.merge:
+        merge_styles(doc, options.merge, target=options.output)
+    else:
+        automatic = options.automatic
+        common = options.common
+        if not automatic ^ common:
+            automatic, common = True, True
+        show_styles(
+            doc,
+            options.output,
+            automatic=automatic,
+            common=common,
+            properties=options.properties,
+        )
+
+
+if __name__ == "__main__":
+    main()
+
+
+
+
+
+
+
+

Functions

+
+
+def delete_styles(document, target, pretty=True) +
+
+
+
+ +Expand source code + +
def delete_styles(document, target, pretty=True):
+    n = document.delete_styles()
+    document.save(target=target, pretty=pretty)
+    printinfo(n, "styles removed (0 error, 0 warning).")
+
+
+
+def find_presentation_list_style(body) +
+
+
+
+ +Expand source code + +
def find_presentation_list_style(body):
+    for frame in body.get_frames(presentation_class="outline"):
+        first_list = frame.get_list()
+        if first_list is not None:
+            return first_list.get_style()
+    return None
+
+
+
+def main() +
+
+
+
+ +Expand source code + +
def main():
+    # Options initialisation
+    usage = "%prog [options] <file>"
+    description = "A command line interface to manipulate styles of OpenDocument files."
+    parser = OptionParser(usage, version=__version__, description=description)
+    # --automatic
+    parser.add_option(
+        "-a",
+        "--automatic",
+        action="store_true",
+        default=False,
+        help="show automatic styles only",
+    )
+    # --common
+    parser.add_option(
+        "-c",
+        "--common",
+        action="store_true",
+        default=False,
+        help="show common styles only",
+    )
+    # --properties
+    parser.add_option(
+        "-p", "--properties", action="store_true", help="show properties of styles"
+    )
+    # --delete
+    msg = "return a copy with all styles (except default) deleted from <file>"
+    parser.add_option("-d", "--delete", action="store_true", help=msg)
+    # --merge
+    msg = (
+        "copy styles from FILE to <file>. Any style with the same "
+        "name will be replaced."
+    )
+    parser.add_option(
+        "-m", "--merge-styles-from", dest="merge", metavar="FILE", help=msg
+    )
+    # --output
+    add_option_output(parser)
+    # Parse options
+    options, args = parser.parse_args()
+    if len(args) != 1:
+        parser.print_help()
+        sys.exit(1)
+    doc = Document(args[0])
+    if options.delete:
+        target = options.output
+        if target is None:
+            printerr(
+                "Will not delete in-place: ", 'output file needed or "-" for stdout'
+            )
+            sys.exit(1)
+        elif target == "-":
+            target = StdoutWriter()
+        else:
+            check_target_file(target)
+        delete_styles(doc, target)
+    elif options.merge:
+        merge_styles(doc, options.merge, target=options.output)
+    else:
+        automatic = options.automatic
+        common = options.common
+        if not automatic ^ common:
+            automatic, common = True, True
+        show_styles(
+            doc,
+            options.output,
+            automatic=automatic,
+            common=common,
+            properties=options.properties,
+        )
+
+
+
+def merge_presentation_styles(document, source) +
+
+
+
+ +Expand source code + +
def merge_presentation_styles(document, source):
+    # Apply master page found
+    source_body = source.body
+    first_page = source_body.get_draw_page()
+    print(first_page.serialize())
+    master_page_name = first_page.master_page
+    print(master_page_name)
+    first_master_page = document.get_style("master-page", master_page_name)
+    printinfo("master page used:", first_master_page.display_name)
+    body = document.get_body
+    for page in body.get_draw_pages():
+        page.set_style_attribute(first_page.get_style())
+        page.set_master_page(first_page.get_master_page())
+        page.set_presentation_page_layout(first_page.get_presentation_page_layout())
+    # Adjust layout -- will obviously work only if content is separated from
+    # style: use of master pages, layout, etc.
+    for presentation_class in ODF_CLASSES:
+        first_frame = source_body.get_frame(presentation_class=presentation_class)
+        if first_frame is None:
+            continue
+        # Mimic frame style
+        position = first_frame.get_position()
+        size = first_frame.size
+        style = first_frame.style
+        presentation_style = first_frame.get_presentation_style()
+        for page in body.get_draw_pages():
+            for frame in page.get_frames(presentation_class=presentation_class):
+                frame.position = position
+                frame.size = size
+                frame.set_style_attribute(style)
+                frame.set_presentation_style(presentation_style)
+        # Mimic list style (XXX only first level)
+        if presentation_class == "outline":
+            list_style = find_presentation_list_style(source_body)
+            for page in body.get_draw_pages():
+                for frame in page.get_frames(presentation_class="outline"):
+                    for lst in frame.get_lists():
+                        lst.set_style_attribute(list_style)
+
+
+
+def merge_styles(document, from_file, target=None, pretty=True) +
+
+
+
+ +Expand source code + +
def merge_styles(document, from_file, target=None, pretty=True):
+    source = Document(from_file)
+    document.delete_styles()
+    document.merge_styles_from(source)
+    doc_type = document.get_type()
+    # Enhance Presentation merge
+    if doc_type in {"presentation", "presentation-template"}:
+        printinfo("merging presentation styles...")
+        merge_presentation_styles(document, source)
+    document.save(target=target, pretty=pretty)
+    printinfo("Done (0 error, 0 warning).")
+
+
+
+def show_styles(document, target=None, automatic=True, common=True, properties=False) +
+
+

Show the different styles of a document and their properties.

+
+ +Expand source code + +
def show_styles(document, target=None, automatic=True, common=True, properties=False):
+    """Show the different styles of a document and their properties."""
+    output = document.show_styles(
+        automatic=automatic, common=common, properties=properties
+    )
+    # Print the output
+    if target is None:
+        print(output, end="")
+        return
+    with open(target, "w") as f:
+        f.write(output)
+
+
+
+
+
+
+
+ +
+ + + \ No newline at end of file diff --git a/doc/templates/index.html b/doc/templates/index.html new file mode 100644 index 0000000..3e342c1 --- /dev/null +++ b/doc/templates/index.html @@ -0,0 +1,53 @@ + + + + + + +odfdo.templates API documentation + + + + + + + + + + + +
+
+
+

Module odfdo.templates

+
+
+
+
+
+
+
+
+
+
+
+
+ +
+ + + \ No newline at end of file