from odfdo import Document, Paragraph document = Document('text') body = document.body paragraph = Paragraph(('A paragraph with a footnote ' 'about references in it.')) body.append(paragraph) # Notes are quite complex so they deserve a dedicated API on paragraphs: paragraph.insert_note(after="graph", note_id='note1', citation="1", body=('Author, A. (2007). "How to cite references", ' 'New York: McGraw-Hill.')) # That looks complex so we detail the arguments: # # after => The word after what the “¹” citation is inserted. # note_id => The unique identifier of the note in the document. # citation => The symbol the user sees to follow the footnote. # body => The footnote itself, at the end of the page. # # odfdo creates footnotes by default. To create endnotes (notes # that appear at the end of the document), give the # note_class=’endnote’ parameter.
# Let's add another section to make our document clear: # Annotations are notes that don’t appear in the document but typically on a # side bar in a desktop application. So they are not printed. from odfdo import Document, Paragraph, Header document = Document("text") body = document.body body.append(Header(1, "Annotations")) paragraph = Paragraph("A paragraph with an annotation in the middle.") body.append(paragraph) # Annotations are inserted like notes but they are simpler: paragraph.insert_annotation(after="annotation", body="It's so easy!", creator="Luis") # Annotation arguments are quite different: # after => The word after what the annotation is inserted. # body => The annotation itself, at the end of the page. # creator => The author of the annotation. # date => A datetime value, by default datetime.now().
from odfdo import Document, Paragraph document = Document("text") body = document.body paragraph = Paragraph(("A paragraph with a footnote " "about references in it.")) body.append(paragraph) # Notes are quite complex so they deserve a dedicated API on paragraphs: paragraph.insert_note( after="graph", note_id="note1", citation="1", body=('Author, A. (2007). "How to cite references", ' "New York: McGraw-Hill."), ) # That looks complex so we detail the arguments: # # after => The word after what the “¹” citation is inserted. # note_id => The unique identifier of the note in the document. # citation => The symbol the user sees to follow the footnote. # body => The footnote itself, at the end of the page. # # odfdo creates footnotes by default. To create endnotes (notes # that appear at the end of the document), give the # note_class=’endnote’ parameter.
from odfdo import Document, Header, Paragraph, Table document = Document("text") body = document.body # Let's add another section to make our document clear: body.append(Header(1, "Tables")) body.append(Paragraph("A 3x3 table:")) # Creating a table : table = Table("Table 1", width=3, height=3) body.append(table)
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 internal_name = 'Pictures/' + filename image = Image.open(path) width, height = image.size paragraph = Paragraph('Standard') # 72 ppp frame = Frame('frame_%d' % numero, 'Graphics', str(int(width / 72.0)) + 'in', str(int(height / 72.0)) + 'in') image = DrawImage(internal_name) frame.append(image) paragraph.append(frame) body.append(paragraph) # And store the data container = document.container with open(path, 'rb') as f: content = f.read() container.set_part(internal_name, content) elif mimetype in ('text/csv', 'text/comma-separated-values'):
# generally want to write paragraphs, lists, headings, and a table of # content to show the document hierarchy at first. # 2.1 - Adding Main Title # Titles are organised by level, starting from level 1. #So let’s add the main title of our document: title1 = Header(1, txt_1) body.append(title1) # 2.2 - Adding more Titles and Paragraphs # title of second level: title = Header(2, txt_a1) body.append(title) # Adding a basic Paragraph of plain text paragraph = Paragraph(txt_a2) body.append(paragraph) # title of second level: title = Header(2, txt_b1) body.append(title) # Adding a basic Paragraph of plain text paragraph = Paragraph(txt_b2) body.append(paragraph) if not os.path.exists('test_output'): os.mkdir('test_output') output = os.path.join('test_output', 'my_basic_text_document.odt')
# 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 paragraph = Paragraph('', style='description') paragraph.append_plain_text(text_1) body.append(paragraph) paragraph = Paragraph(style='line') body.append(paragraph) paragraph = Paragraph(style='smallserif') paragraph.append_plain_text(text_2) body.append(paragraph) paragraph = Paragraph(style='line') body.append(paragraph) paragraph = Paragraph(style='description') paragraph.append_plain_text(text_3)
# 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 paragraph = Paragraph("", style="description") paragraph.append_plain_text(text_1) body.append(paragraph) paragraph = Paragraph(style="line") body.append(paragraph) paragraph = Paragraph(style="smallserif") paragraph.append_plain_text(text_2) body.append(paragraph) paragraph = Paragraph(style="line") body.append(paragraph) paragraph = Paragraph(style="description") paragraph.append_plain_text(text_3)
from odfdo import Document, Paragraph document = Document("text") body = document.body # create a new paragraph with some content : paragraph = Paragraph("Hello_World") body.append(paragraph)
# 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) width, height = image.size draw_size = (f"{width/400:.2f}in", f"{height/400:.2f}in") image_frame = Frame.image_frame(image_uri, size=draw_size, anchor_type="char") paragraph = Paragraph("") paragraph.append(image_frame) body.append(paragraph) body.append(Paragraph("")) elif mimetype in ("text/csv", "text/comma-separated-values"): table = Table(f"table {numero}", style="Standard") csv = reader(open(path)) for line in csv: size = len(line) row = Row() for value in line: cell = Cell(value) row.append_cell(cell) table.append_row(row) for i in range(size): column = Column(style="Standard")
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 Annotation # Annotations are notes that don’t appear in the document but # typically on a side bar in a desktop application. So they are not printed. # Now we add some annotation on each paragraph some_word = paragraph.text_recursive.split()[3] # choosing the 4th word of the paragraph to insert the note paragraph.insert_annotation( after=some_word, # The word after what the annotation is inserted. body="It's so easy!", # The annotation itself, at the end of the page. creator="Bob" # The author of the annotation. # date= xxx A datetime value, by default datetime.now(). )
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"))
body = my_document.body # Create the Table Of Content toc = TOC() # Changing the default "Table Of Content" Title : toc.title = "My Table of Content" # Do not forget to add every components to the document: body.append(toc) # 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)) body.append(paragraph) # Beware, update the TOC with the actual content. If not done there, # the reader will need to "update the table of content" later. toc.fill() if not os.path.exists("test_output"): os.mkdir("test_output") output = os.path.join("test_output", "my_document_with_toc.odt") # And finally save the document. my_document.save(target=output, pretty=True)
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)) product_list.append(item) title12 = Header(2, "Your command") body.append(title12) command = {0: 1, 1: 12, 2: 3, 4: 5} # A table in the text document :
if __name__ == "__main__": document = Document('text') body = document.body print("Making some Koch fractal") title = Header(1, "Some Koch fractal") body.append(title) style = document.get_style('graphic') style.set_properties({"svg:stroke_color": "#0000ff"}) style.set_properties(fill_color="#ffffcc") para = Paragraph('') body.append(para) # some computation of oordinates center, vlist = make_coords(side=12.0, vpos=8.0) # create a circle rad = 8.0 pos = center - complex(rad, rad) circle = EllipseShape(size=('%.2fcm' % (rad * 2), '%.2fcm' % (rad * 2)), position=('%.2fcm' % pos.real, '%.2fcm' % pos.imag)) para.append(circle) # create a drawing with a lot of lines para.append('number of lines: %s' % len(vlist)) for v in vlist:
print("Generating test_output/use_case2.odt ...") # Go document = Document("text") body = document.body # 0- The image # ------------ path = join("samples", "image.png") image = Image.open(path) width, height = image.size # Add the image image_uri = document.add_file(path) draw_size = (f"{width/100:.2f}cm", f"{height/100:.2f}cm") image_frame = Frame.image_frame(image_uri, size=draw_size, position=("0cm", "0cm")) paragraph = Paragraph("", style="Standard") paragraph.append(image_frame) body.append(paragraph) body.append(Paragraph()) # 1- Congratulations (=> style on paragraph) # ------------------------------------------ heading = Header(1, text="Congratulations !") body.append(heading) # The style style = Style( "paragraph", "style1", parent="Standard", area="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 paragraph.insert_note( after=some_word, # The word after what the “¹” citation is inserted. note_id=f"note{p}", # The unique identifier of the note in the document. citation="1", # The symbol the user sees to follow the footnote. body=( f'Author{p}, A. (2007). "How to cite references", Sample Editions.' # The footnote itself, at the end of the page. ), )
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)
if __name__ == "__main__": document = Document("text") body = document.body print("Making some Koch fractal") title = Header(1, "Some Koch fractal") body.append(title) style = document.get_style("graphic") style.set_properties({"svg:stroke_color": "#0000ff"}) style.set_properties(fill_color="#ffffcc") para = Paragraph("") body.append(para) # some computation of oordinates center, vlist = make_coords(side=12.0, vpos=8.0) # create a circle rad = 8.0 pos = center - complex(rad, rad) circle = EllipseShape( size=("%.2fcm" % (rad * 2), "%.2fcm" % (rad * 2)), position=("%.2fcm" % pos.real, "%.2fcm" % pos.imag), ) para.append(circle) # create a drawing with a lot of lines
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'))