21.12.2022 Views

python_para_desenvolvedores_2ed

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

BrOffice.org 315

cursor = doc.Text.createTextCursor()

# Muda as propriedades do texto

cursor.setPropertyValue('CharFontName', 'Verdana')

cursor.setPropertyValue('CharHeight', 20)

cursor.setPropertyValue('CharWeight', 180)

# Insere o texto no documento

doc.Text.insertString(cursor, 'Músicas favoritas\n', 0)

# Cria tabela

tab = doc.createInstance('com.sun.star.text.TextTable')

tab.initialize(rows, cols)

doc.Text.insertTextContent(cursor, tab, 0)

# Preenche a tabela

for row in xrange(rows):

for col in xrange(cols):

cel = chr(ord('A') + col) + str(row + 1)

tab.getCellByName(cel).setString(mus[row][col])

# Propriedades para exportar o documento

props = []

p = PropertyValue()

p.Name = 'Overwrite'

p.Value = True # Sobrescreve o documento anterior

props.append(p)

p = PropertyValue()

p.Name = 'FilterName'

p.Value = 'writer_pdf_Export' # Writer para PDF

props.append(p)

# URL de destino, no qual o arquivo PDF será salvo

url = uno.systemPathToFileUrl(os.path.abspath('musicas.pdf'))

# Salva o documento como PDF

doc.storeToURL(url, tuple(props))

# Fecha o documento

doc.close(True)

Saída (arquivo PDF):

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!