def get_default_doc(): return "dormeur_notstyled.odt" def style_name(): "returns a random style name" return 'rnd%s' % randrange(64) if __name__ == "__main__": try: source = sys.argv[1] except IndexError: source = get_default_doc() document = Document(source) body = document.body print("Add some span styles to", source) # create some random text styles for i in range(64): style = Style( 'text', name='rnd%s' % i, color=(randrange(256), randrange(256), randrange(256)), size="%s" % (8 + i / 5), ) document.insert_style(style) words = set(body.text_recursive.split()) for word in words:
from mimetypes import guess_type from PIL import Image from odfdo import __version__ from odfdo import Document, Frame, Header, Paragraph, Cell, Row from odfdo import Table, Column, FIRST_CHILD # Hello messages print("odfdo installation test") print(f"version : {__version__}") print() print("Generating test_output/use_case1.odt ...") # Go document = Document("text") body = document.body for numero, filename in enumerate(listdir("samples")): # Heading heading = Header(1, text=filename) body.append(heading) path = join("samples", filename) mimetype, _ = guess_type(path) if mimetype is None: mimetype = "application/octet-stream" if mimetype.startswith("image/"): # Add the image image_uri = document.add_file(path) # compute size image = Image.open(path)
#!/usr/bin/env python """ Get the pictures from an .odt file. """ import os from odfdo import Document # ODF export of Wikipedia article Hitchhiker's Guide to the Galaxy (CC-By-SA) # Remark: the document is badly made: the pictures are not displayed in the # text, but are sill inside the document ! filename = "collection.odt" doc = Document(filename) # show the list the content of the document parts parts = doc.get_parts() print("Parts:") print(parts) print() # We want the images of the document. body = doc.body pics = body.get_images() print("Pics :") print(pics) print() # we use the get_part function from odfdo to get the actual content # of the image, to copy the images out of the .odt file: if not os.path.exists("test_output"):
# --merge help = ('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=help) # --output add_option_output(parser) #Parse options options, args = parser.parse_args() if len(args) != 1: parser.print_help() exit(1) document = 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') exit(1) elif target == "-": target = StdoutWriter() else: check_target_file(target) delete_styles(document, target) elif options.merge: merge_styles(document, options.merge, target=options.output) else: automatic = options.automatic
from odfdo import Document, Paragraph document = Document("text") body = document.body # we knwo we have a style of name "highlight" : body.append(Paragraph("Highlighting the word", style="highlight"))
self.price = price def make_catalog(): cat_list = [] price = 10.0 for p in range(5): cat_list.append(Product(chr(65 + p), price)) price += 10.5 return cat_list tax_rate = .196 if __name__ == "__main__": commercial = Document('text') body = commercial.body catalog = make_catalog() title1 = Header(1, "Basic commercial document") body.append(title1) title11 = Header(2, "Available products") body.append(title11) paragraph = Paragraph("Here the list:") body.append(paragraph) # List of products in a list : product_list = List() body.append(product_list) for product in catalog: item = ListItem("%-10s, price: %.2f €" % (product.name, product.price))
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] # 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: 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) else: if options.meta: print(doc.get_formated_meta()) if options.styles: print(doc.show_styles()) # text if doc_type in { 'text', 'text-template', 'presentation', 'presentation-template' }: if options.output: with open(join(output, 'content.rst'), 'w') as f: f.write(doc.get_formatted_text(rst_mode=options.rst)) elif not options.no_content: print(doc.get_formatted_text(rst_mode=options.rst)) # spreadsheet elif doc_type in {'spreadsheet', 'spreadsheet-template'}: if options.output: spreadsheet_to_csv(doc) elif not options.no_content: spreadsheet_to_stdout(doc) else: printerr('The OpenDocument format', doc_type, 'is not supported yet.') sys.exit(1)
#!/usr/bin/env python """ Create a spreadsheet with two tables, using some named ranges. """ import os from odfdo import Document, Table if __name__ == "__main__": document = Document("spreadsheet") body = document.body table = Table("First Table") body.append(table) # populate the table : for i in range(10): table.set_value((1, i), (i + 1)**2) table.set_value("A11", "Total:") # lets define a named range for the 10 values : crange = "B1:B10" name = "squares_values" table_name = table.name table.set_named_range(name, crange, table_name) # we can define a single cell range, using notation "B11" or (1, 10) : table.set_named_range("total", (1, 10), table_name) # get named range values : values = table.get_named_range("squares_values").get_values(flat=True) # set named range value :
def main(): usage = 'usage: %prog -i IMAGE -r RANGE -s SIZE PRESENTATION' description = 'Add an image on some pages of a presentation.' parser = OptionParser(usage, description=description) parser.add_option('-i', '--image', dest='image', help='Image to be added', action='store', type='string') parser.add_option('-r', '--range', dest='range', help='Range of the slides', action='store', type='string') parser.add_option('-s', '--size', dest='size', help='max width in cm of the image', action='store', type='float') options, source = parser.parse_args() if not source or not options.image or not options.range or not options.size: print('need options !') parser.print_help() exit(0) lst = options.range.split('-') start = int(lst[0]) - 1 end = int(lst[1]) file_name = source[0] image_size = make_image_size(options.image, float(options.size)) presentation = Document(file_name) presentation_body = presentation.body uri = presentation.add_file(options.image) # Append all the component for i in range(start, end): # Create a frame for the image image_frame = Frame.image_frame( image=uri, text='', # Text over the image object size=image_size, # Display size of image anchor_type='page', page_number=None, position=image_position, style=None) image_frame.svg_title = title image_frame.svg_description = text slide = presentation_body.get_draw_page(position=i) slide.append(image_frame) # Finally save the result name_parts = file_name.split('.') name_parts.insert(-1, modified_file_suffix) new_name = '.'.join(name_parts) presentation.save(new_name)
action="store", type="string", ) options, sources = parser.parse_args() if not sources: print("need presentations sources !") parser.print_help() sys.exit(0) if options.output: concat_filename = options.output else: concat_filename = default_concat_filename t0 = time.time() # prepare destination file concat_presentation = Document("presentation") concat_presentation.delete_styles() for source in sources: if os.path.isdir(source): for root, dirs, files in os.walk(source): for name in files: parse_odp(os.path.join(root, name), concat_presentation) else: parse_odp(source, concat_presentation) concat_presentation.save(target=concat_filename, pretty=True) elapsed = int(time.time() - t0) nb_slides = len(concat_presentation.body.get_draw_pages()) print("%s presentations concatenated in %s (%s slides) in %ss." % (counter_odp, concat_filename, nb_slides, elapsed))
# Authors: Jerome Dumonteil <*****@*****.**> import os from optparse import OptionParser from sys import exit from odfdo import __version__ from odfdo import Document if __name__ == '__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() 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)
# return document # This the .odt file with some pictures in it we will display on a presentation # filename = "collection.odt" # ODF export of Wikipedia article Hitchhiker's Guide to the Galaxy (CC-By-SA) filename = "collection.odt" # For convenience, take it from the remote URI: #filename="http://www.odfgr.org/wiki/example-wikipedia-article/;download" # We will copy all image from collection.odt, build a presentation with the # images and save it in this file: output_filename = "my_presentation_of_text_picts.odp" # Open the input document # doc_source = Document_extend(filename) doc_source = Document(filename) # Making of the output Presentation document : presentation_output = Document('presentation') # Presentation got a body in which elements are stored presentation_body = presentation_output.body presentation_manifest = presentation_output.get_part('manifest') # For each image, we create a page in the presentation and display the image # and some text on this frame # First, get all image elements available in document: images_source = doc_source.body.get_images() manifest_source = doc_source.get_part('manifest') for image in images_source:
from odfdo import Document, Paragraph, Frame doc = Document('text') body = doc.body uri = doc.add_file('newlogo.png') image_frame = Frame.image_frame(uri, size=('6cm', '4cm'), position=('5cm', '10cm')) # put image frame in a paragraph: para = Paragraph('') para.append(image_frame) body.append(para) # doc.save(target='test_picture.odt', pretty=True)
crop_size = True output_file = options.output if not output_file.endswith(".odp"): output_file += ".odp" # Collecting images collect_sources(sources) if not images_pool: # unable to find images print("no image found !") parser.print_help() exit(0) # Creation of the output Presentation document : # presentation = Document_from_type('presentation') # 092 presentation = Document('presentation') # Presentation got a body in which content is stored presentation_body = presentation.body # For each image, we create a page in the presentation and display the image # and some text on this frame for image in images_pool: # add the file to the document uri = presentation.add_file(image.path) # Create an underlying page for the image and the text page = DrawPage("Page " + image.name) # Create a frame for the image image_frame = Frame.image_frame(
element.append(child) if tail is not None: if type(element) == type([]): element.append(tail) else: element.tail = tail return (element, True) if __name__ == "__main__": try: source = sys.argv[1] except IndexError: source = get_default_doc() document = Document(source) body = document.body print("Moving links to footnotes from", source) print("links occurrences:", len(body.get_links())) print("footnotes occurences:", len(body.get_notes())) counter_links_in_notes = 0 for note in body.get_notes(): for link in note.get_links(): counter_links_in_notes += 1 url = link.get_attribute("xlink:href") text = link.text_recursive tail = link.tail new_tail = " (link: %s) %s" % (url, tail) link.tail = new_tail
from os import mkdir from os.path import join, exists from odfdo import __version__ from odfdo import Document, Table, Row, Cell, Style, rgb2hex from odfdo import create_table_cell_style from odfdo import make_table_cell_border_string # Hello messages print('odfdo installation test') print(' Version : %s' % __version__) print() print('Generating color chart in my_color_chart.ods ...') document = Document('spreadsheet') body = document.body table = Table('chart') for y in range(0, 256, 8): row = Row() for x in range(0, 256, 32): cell_value = (x, y, (x + y) % 256) border_rl = make_table_cell_border_string(thick='0.20cm', color='white') border_bt = make_table_cell_border_string( thick='0.80cm', color='white', ) style = create_table_cell_style( color='grey',
'-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)
from odfdo import Document, List document = Document('text') body = document.body my_list = List(['chocolat', 'café']) # In case your forgot to insert an important item: my_list.insert_item("Chicorée", position=1) # Or you can insert it before another item: cafe = my_list.get_item(content="café") my_list.insert_item('Chicorée', before=cafe) # Or after: my_list.insert_item("Chicorée", after=cafe)
lst = """123 azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop azertyuiop end. """.splitlines() output_filename = "my_generated_presentation.odp" presentation = Document('presentation') presentation_body = presentation.body # Creating a smooth style for the graphic item base_style = Style( 'graphic', name='Gloup48', parent="standard", stroke="none", fill_color="#b3b3b3", textarea_vertical_align="middle", padding_top="1cm", padding_bottom="1cm", padding_left="1cm", padding_right="1cm",
from odfdo import Document def get_default_doc(): # ODF export of Wikipedia article Hitchhiker's Guide to the Galaxy (CC-By-SA) return "collection2.odt" if __name__ == "__main__": try: source = sys.argv[1] except IndexError: source = get_default_doc() # To illustrate metadata introspection, let’s open an existing document: document = Document(source) # Metadata are accessible through the meta part: meta = document.get_part("meta.xml") # You then get access to various getters and setters. The getters return # Python types and the respective setters take the same Python type as # a parameter. # # Here are the output of the get_xxx methods for metadata. # (Notice that LpOD doesn’t increment editing cycles nor statistics # when saving the document. # For the metadata using dates or durations, lpOD provides datatypes that # decode from and serialize back to strings. # Strings are always decoded as unicode, numeric values are always decoded # as Decimal (as they offer the best precision).
#!/usr/bin/env python """ Read the text content from an .odt file. """ # Import from odfdo from odfdo import Document # ODF export of Wikipedia article Hitchhiker's Guide to the Galaxy (CC-By-SA) filename = "collection2.odt" doc = Document(filename) # just verify what type of document it is: print("Type of document:", doc.get_type()) body = doc.body print(body) # to further manipulate the document, you can access to whole xml content: content = doc.get_part('content.xml') print(content) # A quick way to get the text content: text = doc.get_formatted_text() print("Size :", len(text)) # Let's show the beginning : print(text[:320]) expected_result = """
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)
from odfdo import Document, Table document = Document("spreadsheet") body = document.body table = Table("Empty Table") table.set_value("A1", "Hello World") body.append(table)
#!/usr/bin/env python """ Create a new presentation from a previous one by extrating some slides, in a different order. """ import os import sys from odfdo import Document filename = "presentation_base.odp" presentation_base = Document(filename) output_filename = "my_extracted_slides.odp" if __name__ == "__main__": # extract = sys.argv[1:] extract = " 3 5 2 2" extracted = Document("presentation") body_base = presentation_base.body body_extracted = extracted.body # Important, copy styles too: extracted.delete_styles() extracted.merge_styles_from(presentation_base) for i in extract.split(): try: slide_position = int(i) - 1 slide = body_base.get_draw_page(position=slide_position)
# Authors: Jerome Dumonteil <*****@*****.**> from os import mkdir from os.path import join, exists from odfdo import __version__ from odfdo import Document, Table, Cell, Row, Style, rgb2hex from odfdo import create_table_cell_style, make_table_cell_border_string # Hello messages print("odfdo installation test") print(" Version : %s" % __version__) print() print("Generating test_output/use_case3.ods ...") document = Document("spreadsheet") body = document.body table = Table("use_case3") for y in range(0, 256, 8): row = Row() for x in range(0, 256, 32): cell_value = (x, y, (x + y) % 256) border_rl = make_table_cell_border_string(thick="0.20cm", color="white") border_bt = make_table_cell_border_string( thick="0.80cm", color="white", ) style = create_table_cell_style( color="grey",
#!/usr/bin/env python from odfdo import Document # ODF export of Wikipedia article Hitchhiker's Guide to the Galaxy (CC-By-SA) : filename = "collection2.odt" doc = Document(filename) # The body object is an XML element from which we can access one or several # other elements we are looking for. body = doc.body # Should you be lost, remember elements are part of an XML tree: mypara = body.get_paragraph(position=42) print("children of the praragraph:", mypara.children) print("parent of the paragraph:", mypara.parent) # And you can introspect any element as serialized XML: link0 = body.get_link(position=0) print("Content of the serialization link:") print(link0.serialize()) print("which is different from the text content of the link:") print(link0.text_recursive)
import urllib.request, urllib.parse, urllib.error def random_text(sentences): uri = "http://enneagon.org/phrases/%s" % sentences try: text = urllib.request.urlopen(uri).read().decode("iso8859-1") except: text = "Almost no text." return text from odfdo import Document, Header, Paragraph # Create the document my_document = Document('text') body = my_document.body # Add content (See Create_a_basic_document.py) title1 = Header(1, random_text(1)[:70]) body.append(title1) for p in range(3): title = Header(2, random_text(1)[:70]) body.append(title) paragraph = Paragraph(random_text(10)) # Adding Footnote # Now we add a footnote on each paragraph # Notes are quite complex so they deserve a dedicated API on paragraphs: some_word = paragraph.text_recursive.split()[3] # choosing the 4th word of the paragraph to insert the note
'in faucibus orci luctus et ultrices ' 'posuere cubilia Curae; Aliquam nibh.') text_3 = ('\n' 'Duis semper. \n\tDuis arcu massa,' ' \n\t\tscelerisque vitae, \n' '\t\t\tconsequat in, \n' '\t\t\t\tpretium a, enim. \n' '\t\t\t\t\tPellentesque congue. \n' 'Ut in risus volutpat libero pharetra ' 'tempor. Cras vestibulum bibendum augue.' 'Praesent egestas leo in pede. Praesent ' 'blandit odio eu enim. Pellentesque sed') # make document, document = Document('text') # remove default styles document.delete_styles() # add our styles document.insert_style(_style_font_1, default=True) document.insert_style(_style_font_2, default=True) document.insert_style(_style_font_3, default=True) document.insert_style(_style_page, automatic=True) document.insert_style(_style_master) document.insert_style(_style_footer) document.insert_style(_style_description) document.insert_style(_style_small_serif) document.insert_style(_style_bold) body = document.body
Create a table of 1000 lines and 100 columns, extract a sub table of 100 lines 26 columns, save the result in a spreadsheet document. """ import os from odfdo import Document, Table, Row, Cell def suite(n): if n % 2 == 0: return n / 2 return 3 * n + 1 if __name__ == "__main__": spreadsheet = Document('spreadsheet') # Populate the table in the spreadsheet body = spreadsheet.body table = Table("Big Table") body.append(table) lines = 1000 cols = 100 for line in range(lines): row = Row() values = [] n = line for i in range(cols): values.append(n)
def main(): usage = "usage: %prog -i IMAGE -r RANGE -s SIZE PRESENTATION" description = "Add an image on some pages of a presentation." parser = OptionParser(usage, description=description) parser.add_option( "-i", "--image", dest="image", help="Image to be added", action="store", type="string", ) parser.add_option( "-r", "--range", dest="range", help="Range of the slides", action="store", type="string", ) parser.add_option( "-s", "--size", dest="size", help="max width in cm of the image", action="store", type="float", ) options, source = parser.parse_args() if not source or not options.image or not options.range or not options.size: print("need options !") parser.print_help() exit(0) lst = options.range.split("-") start = int(lst[0]) - 1 end = int(lst[1]) file_name = source[0] image_size = make_image_size(options.image, float(options.size)) presentation = Document(file_name) presentation_body = presentation.body uri = presentation.add_file(options.image) # Append all the component for i in range(start, end): # Create a frame for the image image_frame = Frame.image_frame( image=uri, text="", # Text over the image object size=image_size, # Display size of image anchor_type="page", page_number=None, position=image_position, style=None, ) image_frame.svg_title = title image_frame.svg_description = text slide = presentation_body.get_draw_page(position=i) slide.append(image_frame) # Finally save the result name_parts = file_name.split(".") name_parts.insert(-1, modified_file_suffix) new_name = ".".join(name_parts) presentation.save(new_name)