text = document_source.body.text_recursive for c in "():;!.,[]{}#@/\\=-_+*#@`\"'": text = text.replace(c, " ") # slow algorithm words = text.split() print("nb of words:", len(words)) frequences = {} for word in words: frequences[word] = frequences.get(word, 0) + 1 print("unique words found:", len(frequences)) # Populate the table in the spreadsheet body = spreadsheet.body table = Table("Frequency Table") body.append(table) sorted = [(value, key) for key, value in frequences.items()] sorted.sort() sorted.reverse() # one solution : # for value, key in sorted: # row = Row() # row.set_value(0, key) # row.set_value(1, value) # Cell type is guessed. # table.append_row(row) # another solution :
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', background_color=cell_value, border_right=border_rl,
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)
# 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'): table = Table("table %d" % 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") table.insert(column, FIRST_CHILD) body.append(table) else: paragraph = Paragraph("Not image / csv", style="Standard") body.append(paragraph)
from odfdo import Document, Table document = Document('spreadsheet') body = document.body table = Table("Empty Table") table.set_value('A1', "Hello World") body.append(table)
# 2- Your environment (=> a table) # -------------------------------- heading = Header(1, text="Your environment") body.append(heading) data = [] # lpOD Version data.append(["odfdo library version", __version__]) # Python version data.append(["Python version", "%d.%d.%d" % version_info[:3]]) # Creation / Insertion table = Table("table1", width=2, height=2, style="Standard") table.set_values(data) body.append(table) # 3- Description (=> footnote & => highlight) # ------------------------------------------- heading = Header(1, text="Description") body.append(heading) # A paragraph with a note text = "The odfdo project is made to generate easily OpenDocuments." paragraph = Paragraph(text, style="Standard") paragraph.insert_note( after="odfdo project", note_id="note1", citation="1", body="http://xxxxxxxxxxxx/" )
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) n = suite(n) row.set_values(values) table.append(row)
#!/usr/bin/env python """ Transpose a table. Create a spreadsheet table (example: 50 rows and 20 columns), and subsequently create a new table in a separate sheet where the columns and rows are now swapped (e.g. 20 rows and 50 columns). """ import os from odfdo import Document, Table, Row if __name__ == "__main__": spreadsheet = Document("spreadsheet") # Populate the table in the spreadsheet body = spreadsheet.body table = Table("Table") body.append(table) lines = 50 cols = 20 for line in range(lines): row = Row() for column in range(cols): row.set_value(column, "%s%s" % (chr(65 + column), line + 1)) table.append(row) print("Size of Table :", table.size) table2 = Table("Symetry")
from odfdo import Document, Table document = Document("spreadsheet") body = document.body table = Table("Empty Table") table.set_value("A1", "Hello World") body.append(table)
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") table.insert(column, FIRST_CHILD) body.append(table) else: paragraph = Paragraph("Not image / csv", style="Standard") body.append(paragraph)
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 : table = Table("Table") body.append(table) row = Row() row.set_values(["Product", "Price", "Quantity", "Amount"]) table.set_row("A1", row) # or: table.set_row(0, row) row_number = 0 for item, quantity in command.items(): prod = catalog[item] row = Row() row.set_value("A", prod.name) #or : row.set_value(0, prod.name) cell = Cell() cell.set_value(prod.price, text="%.2f €" % prod.price, currency="EUR",
""" import os from odfdo import Document, Table if __name__ == "__main__": # creating an empty spreadsheet document: document = Document('spreadsheet') # Each sheet of a spreadsheet is a table: # setting drom the beginning width (columns) and height (rows) # is not mandatory, but a good practice, since odfdo don't check # actual existence of cells body = document.body table = Table("First Table", width=20, height=3) body.append(table) # A table contains rows, we can append some more. for r in range(2): table.append_row() print("rows in the table (3+2):", len(table.get_rows())) # A row contains cells for row in table.get_rows(): print("row, nb of cells ", row.y, len(row.get_cells())) last_row = table.get_row(-1) print("nb of cells of the last row:", len(last_row.get_cells())) # cell can have different kind of values
#!/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 :
#!/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 :
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", background_color=cell_value, border_right=border_rl,