Python/C++, PyQt4/Qt4: How to insert multiple lines text into a cell in QTextTable

449 views Asked by At

I'm trying to create a odt document by using PyQt4. The document involves inserting multiple lines text into cells. My example code:

from PyQt4 import QtGui

doc = QtGui.QTextDocument()
cur = QtGui.QTextCursor(doc)
table = cur.insertTable(1, 2)
# The cursor is moved to the first cell of the table
cur.insertText("""line 1
line 2""")

writer = QtGui.QTextDocumentWriter()
writer.setFormat(writer.supportedDocumentFormats()[1]) # ODF Format
writer.setFileName('example.odt')
writer.write(doc)

I want 'line 1' and 'line 2' to be in the same cell, but they are in different cells due to the line break. This also had the same effect as the code above:

cur.insertText('line 1\n')
cur.insertText('line 2\n')
1

There are 1 answers

0
RBenet On

Perhaps it’s too late.

If you use chr(10) you insert a line feed. There are two ways to do it.

cur.insertText(‘line1’)
cur.insertText(chr(10))
cur.insertText(‘line2’+chr(10))
cur.insertText(‘line3’)