Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am creating a Word document (.docx) using LibreOffice. I need to include some numeric results I calculated in R. They look like a simple table of numbers, with text captions for the rows and columns, and a few NAs here and there (a string representing R's "missing value" constant).

I tried saving the table to disk in a .csv format, opening it in LibreOffice Calc, and copy-pasting it into Writer. It got inserted as an OLE object. This has several problems. First, this means that the document is big and slow to open. Second, I am afraid of compatibility problems (I don't trust that the OLE object will work fine when somebody else reads this thing with Word instead of LibreOffice). Third, it formats the numbers in the table as numbers (right-adjusted), but the NA values as text (left-adjusted), and also all the numbers on a row containing an NA as text. So it is very hard to read, as the columns don't line up.

I know I could create a table in LibreOffice and copy-paste the numbers there. But is there a quicker solution, maybe some Word addon which automatically creates tables from .csv, or maybe a way to save the R output as a HTML table (those get formatted automatically when pasted in LibreOffice)? I don't need fancy looks as long as everything lines up and creates a native Word table.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You could just open the .csv in LibreOffice (writer), select all and click on the table wizard thingie:

enter image description here

In my case, this resulted in:

enter image description here

As for the HTML way, the following PERL script will do what you need (I am assuming you are on Linux and have a space separated .csv file):

#!/usr/bin/perl -w
print <<EOF;

border:1px solid black;

    print "<tr>";
    my @a=split(/\s+/); 
    print "<td>$_</td>" foreach (@a);
    print "</tr>\n";
print "</table></body></html>\n";

Save the script as csv2html.pl and run it like so:

$ perl csv2html.pl input.csv > output.html
share|improve this answer
The first version does not work at all for me (LibreOffice First, I can't open the .csv file in Writer; when I select Open from a Writer window, it gets opened in a Calc window instead. When I paste the contents into a new Writer document as text, the Wizard creates a table of one row per csv row, but puts all the contents of this row into the same cell. I suppose that this is because my .csv uses ; as separators. Luckily, your Perl script worked beautifully once I corrected the separator in the regex. Thank you very much! Looks like I need to learn a scripting language. –  rumtscho Sep 23 '12 at 12:23
@rumtscho, yup, scripting is useful :). You can do it in libreoffice though. Just change or remove the .csv extension from your file. Try .txt or just no extension (extensions are optional in linux). Then LibreOffice will open the file in Writer and you can use the table wizard. –  terdon Sep 23 '12 at 12:39
To make it clear: I got it into libreoffice, but the Table Wizard didn't work, not recognizing my semicolons as separators. I guess I could write a script which changes all semicolons to whitespace first (I probably know enough bash for that, though no perl), but given that a script can create the table itself (as you already showed), this totally violates my idea of efficiency. Still, thank you for showing me both variants, it was valuable information which I maybe can use some other time. –  rumtscho Sep 23 '12 at 12:49
add comment

It turned out that LibreOffice also has a Paste Special functionality (duh). It allows me to choose "formatted text" or "HTML" instead of "calc8" when pasting a table copied from Calc. I still prefer the script solution, because Paste Special uses the formatting present in Calc, where rows with an NA are aligned left and rows with numbers only are aligned right.

share|improve this answer
This answer was very helpful to me. I wouldn't have though to select "HTML" for CSV/table data. –  dsh Feb 25 '13 at 22:17
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.