Home

Tags

Создание OpenOffice Calc (ods) из python

2011-07-16 python openoffice ods calc

Для работы с файлам ods существует библиотека ooolib

Установка ooolib

$ svn co https://ooolib.svn.sourceforge.net/svnroot/ooolib ooolib
$ cd ooolib/ooolib-python
$ sudo python setup.py install

Примеры использования

import ooolib

# Create your document
doc = ooolib.Calc()

# Set values.  The values are set in column, row order, but the values are
# not in the traditional "A5" style format.  Instead we require two integers.
# set_cell_value(col, row, datatype, value)
for row in range(1, 9):
	for col in range(1, 9):
		doc.set_cell_value(col, row, "float", col * row)

# Save the document to the file you want to create
doc.save("calc-example01.ods")


import ooolib

# Create the document
doc = ooolib.Calc()

# Set Column Width
doc.set_column_property(1, 'width', '0.5in')
doc.set_column_property(2, 'width', '1.0in')
doc.set_column_property(3, 'width', '1.5in')

# Set Row Height
doc.set_row_property(1, 'height', '0.5in')
doc.set_row_property(2, 'height', '1.0in')
doc.set_row_property(3, 'height', '1.5in')

# Fill in Cell Data
doc.set_cell_value(1, 1, "string", "0.5in x 0.5in")
doc.set_cell_value(2, 2, "string", "1.0in x 1.0in")
doc.set_cell_value(3, 3, "string", "1.5in x 1.5in")

# Write out the document
doc.save("calc-example04.ods")


import ooolib

# Create the document
doc = ooolib.Calc()

# Standard Cell Properties
# Cell Properties are handled by using on/off switches
# Turn the switch on, then do all the cells you want, then
# turn the switch back off.

doc.set_cell_value(1, 1, "string", "Normal Text")

doc.set_cell_property('bold', True)
doc.set_cell_value(1, 2, "string", "Bold Text")
doc.set_cell_property('bold', False)

doc.set_cell_property('italic', True)
doc.set_cell_value(1, 3, "string", "Italic Text")
doc.set_cell_property('italic', False)

doc.set_cell_property('underline', True)
doc.set_cell_value(1, 4, "string", "Underline Text")
doc.set_cell_property('underline', False)


# Colors
# Colors are in the format '#ffffff'.  If you use an
# incorrect format, the color will be changed to the
# default color.
doc.set_cell_property('color', '#0000ff')
doc.set_cell_property('background', '#ff0000')
doc.set_cell_value(2, 1, "string", "Blue on Red")

doc.set_cell_property('color', '#ff0000')
doc.set_cell_property('background', '#0000ff')
doc.set_cell_value(2, 2, "string", "Red on Blue")

doc.set_cell_property('color', 'default')
doc.set_cell_property('background', 'default')
doc.set_cell_value(2, 3, "string", "Default Colors")


# Text Font Sizes
doc.set_cell_property('fontsize', '10')
doc.set_cell_value(3, 1, "string", "Default 10pt")

doc.set_cell_property('fontsize', '11')
doc.set_cell_value(3, 2, "string", "11pt")

doc.set_cell_property('fontsize', '12')
doc.set_cell_value(3, 3, "string", "12pt")

doc.set_cell_property('fontsize', '10')

# Write out the document
doc.save("calc-example05.ods")


import ooolib

# Create your document
doc = ooolib.Calc()

for row in range(1, 9):
	doc.set_cell_value(1, row, "float", row)

doc.set_cell_value(2, 1, 'string', 'AVERAGE')
doc.set_cell_value(2, 2, 'string', 'MIN')
doc.set_cell_value(2, 3, 'string', 'MAX')
doc.set_cell_value(2, 4, 'string', 'SUM')
doc.set_cell_value(2, 5, 'string', 'SQRT')
doc.set_cell_value(2, 6, 'string', 'Condition')

doc.set_cell_value(3, 1, 'formula', '=AVERAGE(A1:A8)')
doc.set_cell_value(3, 2, 'formula', '=MIN(A1:A8)')
doc.set_cell_value(3, 3, 'formula', '=MAX(A1:A8)')
doc.set_cell_value(3, 4, 'formula', '=SUM(A1:A8)')
doc.set_cell_value(3, 5, 'formula', '=SQRT(A8)')
doc.set_cell_value(3, 6, 'formula', '=IF((A5>A4);A4;"")')

# Save the document to the file you want to create
doc.save("calc-example06.ods")