OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Convert All Tables in Document to Text

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Writer
View previous topic :: View next topic  
Author Message
melbish
General User
General User


Joined: 05 Sep 2012
Posts: 6

PostPosted: Sat Sep 08, 2012 7:44 pm    Post subject: Convert All Tables in Document to Text Reply with quote

Now that I seem to be getting the hang of regular expressions, I am moving into the next part of my project, which involves re-formatting imported RTF documents for publication.

The original docs are RTF files from an exam-generator software app, and they are quizzes with matching questions. The answer bank section of each question is auto-formatted by the exam software as a table, with one column for the answer letter & one for the data: so 2 columns wide & 20 rows.

a man
b dog
c cat

There are 20 answer choices, and I want them to be in 4 columns. I can't apply columns to the table unless I convert it, but when my files have 5-7 tables that need converting, I start nostalgically thinking about Word and VBA and how I know the exact macro I could run in Word that would do this very thing. But I don't know how to do macros in OOO yet, which I would love to learn.

And the next part of it is that the questions need to be re-marginized, if that's a word, and then put into columns of their own. On this one test I'm doing now, there are 6 groups of questions with 15 questions/20 answer choices, and 1 with 10 questions/15 answers. So 7 total, and I didn't like how long it took me to go and manually reformat the thing.

AND - one other thing - is there a way to say put text into columns and where there is a blank line inserted between lines of text, have that trigger a column break when I turn it into columns? Still working on that one.

Is there a way to do this? Somehow? In some amazing parallel universe where OOo really is as accessible as I want it to be, because I'm beginning to really realize how significant its advantages over Microsoft Word are. I think.

Thanks!!
_________________
Melissa Bishop
Creative Classical Curriculum
www.latin-resources.com
Back to top
View user's profile Send private message
floris_v
Moderator
Moderator


Joined: 12 Jul 2007
Posts: 4791
Location: Netherlands

PostPosted: Sun Sep 09, 2012 12:45 am    Post subject: Reply with quote

When it comes to ease of macro programming, MS Word wins hands down.
_________________
LibreOffice 3.6.3; OOo 3.4.1 on Windows Vista
Join the Official community forum - in several languages, including Nederlandstalig forum
Back to top
View user's profile Send private message
Robert Tucker
Moderator
Moderator


Joined: 16 Aug 2004
Posts: 3407
Location: Manchester UK

PostPosted: Sun Sep 09, 2012 1:40 pm    Post subject: Reply with quote



Produced by this macro (before it deleted all the tables):

Code:
Sub TableText

Dim oCell(2,20)
Dim oCellString(20)

oTextTables = ThisComponent.getTextTables
    If NOT oTextTables.hasElements() Then Exit Sub
    For i = 0 To oTextTables.getCount() - 1
      ' Loop through tables
      oCellString(i) = ""
      oTable = oTextTables.getByIndex(i)
      oRows = oTable.getRows
      oColumns  = oTable.getColumns
      iRowCount = oRows.Count
      ' Loop through Cells
      For iRow = 0 to iRowCount - 1
         For iCol = 0 to 1
            oCell(iCol,iRow) = oTable.getCellByPosition(iCol, iRow)
         Next iCol   
      Next iRow
     
    q = 0
    For m = 0 to 3
    For n = 0 to 16 step 4
    q = m + n
    If n = 16 then
    oCellString(i) = oCellString(i) + oCell(0,q).getString() + "   " + oCell(1,q).getString()
    Else
    oCellString(i) = oCellString(i) + oCell(0,q).getString() + "   " + oCell(1,q).getString() + "      "
    End if
    Next n
   oCellString(i) = oCellString(i) + chr(13)
   Next m
      oText = oCell(1,19).getText()
   oCurs = oText.createTextCursor()
   oCurs.gotoEnd(False)
   oPar = ThisComponent.createInstance("com.sun.star.text.Paragraph")
   oPar.setstring(oCellString(i))
   oText.insertTextContentAfter ( oPar, oTable )
   Next i
   
oTextTables = ThisComponent.getTextTables
lCount = oTextTables.getCount()
Do While lCount <> 0
oTable = oTextTables.getByIndex(0)
oTable.dispose()
oTextTables = ThisComponent.getTextTables
lCount = oTextTables.getCount()
Loop
   
End Sub


Again it's just a matter of adding or deleting a few tabs to get it right. I had a bit of a problem with it seeming to print things over and over with variations but the last few tests went OK; so try it if you like, but be careful!
_________________
OpenOffice 4.0.0 and LibreOffice 4.x.x on Fedora 20, Ubuntu 13.10, Windows 8.1 Preview (Triple Boot)
Back to top
View user's profile Send private message
Robert Tucker
Moderator
Moderator


Joined: 16 Aug 2004
Posts: 3407
Location: Manchester UK

PostPosted: Sun Sep 09, 2012 2:05 pm    Post subject: Reply with quote



produced by this macro:

Code:
Sub TableText

Dim oCell(2,20)
Dim oCellString(20)

oTextTables = ThisComponent.getTextTables
    If NOT oTextTables.hasElements() Then Exit Sub
    For i = 0 To oTextTables.getCount() - 1
      ' Loop through tables
      oCellString(i) = ""
      oTable = oTextTables.getByIndex(i)
      oRows = oTable.getRows
      oColumns  = oTable.getColumns
      iRowCount = oRows.Count
      ' Loop through Cells
      For iRow = 0 to iRowCount - 1
         For iCol = 0 to 1
            oCell(iCol,iRow) = oTable.getCellByPosition(iCol, iRow)
         Next iCol   
      Next iRow
     
    q = 0
    For m = 0 to 4
    For n = 0 to 15 step 5
    q = m + n
    If n = 12 then
    oCellString(i) = oCellString(i) + oCell(0,q).getString() + "   " + oCell(1,q).getString()
    Else
    oCellString(i) = oCellString(i) + oCell(0,q).getString() + "   " + oCell(1,q).getString() + "      "
    End if
    Next n
   oCellString(i) = oCellString(i) + chr(13)
   Next m
      oText = oCell(1,19).getText()
   oCurs = oText.createTextCursor()
   oCurs.gotoEnd(False)
   oPar = ThisComponent.createInstance("com.sun.star.text.Paragraph")
   oPar.setstring(oCellString(i))
   oText.insertTextContentAfter ( oPar, oTable )
   Next i
   
oTextTables = ThisComponent.getTextTables
lCount = oTextTables.getCount()
Do While lCount <> 0
oTable = oTextTables.getByIndex(0)
oTable.dispose()
oTextTables = ThisComponent.getTextTables
lCount = oTextTables.getCount()
Loop

End Sub


I think in this case it's just a matter of deleting the tab before "j bird" and the tab before "n vole" to get it straight. It's set to work on tables 2 columns wide and 20 rows long so you may get some rubbish out if you have tables of other dimensions.
_________________
OpenOffice 4.0.0 and LibreOffice 4.x.x on Fedora 20, Ubuntu 13.10, Windows 8.1 Preview (Triple Boot)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Writer All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group