This is a Libreoffice Writer extension that counts words in each section of a document and displays each wordcount in the status bar or inserted into the document as a field. Its purpose is to let you know when you're nearly done writing each section. For example: Abstract, Essay, Bibliography, and Footnotes. It is released under the MIT license.
- Mark sections by simply adding a bookmark at the start of each section using the Libreoffice Writer menu option
Insert -> Bookmark(These can be viewed later in the navigator sidebar by pressing F5). - Then simply type Ctrl-Alt-W (or Cmd-Alt-W on Mac) and it will display the wordcount for each section in the status bar for 4 seconds.
- Ctrl-Alt-W will also create wordcount fields that you can insert into the document with
Ctrl-F2 -> Variables -> User Field. If there are bookmarks, the wordcount fields will also be updated every time the document is saved. - You can even enter formulas based on the wordcounts. If, for example, you have a bookmark called
Essay, then to show footnotes as a percentage of Essay length, pressCtrl-F2 -> Variables -> Insert Formulaand type into the Formula boxWordcount_Footnotes/Wordcount_Essaythen format it like a percentage in the Format box by clickingAdditional Formatsand finally clickInsertto insert it into your document. - If you have so many sections that the word counts disappear off the end of the status bar, here is a way to display them in a cover page or footer instead.
The word counter uses the same word separator as Libreoffice, so it's accurate, compared to both Libreoffice and MS Word, with the following exceptions:
- If your document has bullets/numbering, the extension's wordcount will be low by one count per bullet compared to Libreoffice's total wordcount: see second screenshot. (If you know how I can fix this, let me know by posting an issue. I'm using counting words in textCursor.getString() which does not contain bullets.)
- Libreoffice completely ignores superscripted footnote references (as it should), whereas Word incorrectly count them as separate words if they are followed by punctuation or surrounded by space. Normally you should not follow footnote references by punctuation, but one valid case is right before the end of a parenthesis.
- Once I found an obscure character in a document that caused Libreoffice to stop counting words until the end of the line. The character was unicode '\u200b' but I do not know how it got there.
- This script assumes that the Writer setting at
Tools -> Options -> Writer -> General -> Wordcount -> Additional Separatorscontains only n-dash and m-dash (which is the default). If the user has added any more characters, they will not be taken into account because I don't know how to fetch this setting into my script. - If you place a bookmark in the middle of the word, that will effectively break that word in two, counting it twice in this extension's total (though not in Libreoffice's total wordcount)
These tests were done on Libreoffice 7.4.4.2 and MS Word versions 2007 and Office 365 online as at Jan 2023.
Download Wordcounter.oxt from the repository and double-click it. It should automatically open Libreoffice and install.
If that does not work, then run Writer and click Tools -> Extension Manager -> Addand then browse to and select the .oxt file to install it.
- Rename the .oxt file to .zip and unzip it.
- Inside the zipped file, edit the
Accelerators.xcufile and change the line that says"W_MOD1_MOD2"to be something else. Mod1 stands for Ctrl/Cmd and Mod2 stands for Alt. - Zip up the directory again and rename it back to .oxt
The core of the extension is a Libreoffice BASIC macro.
If you wish to alter the macro:
- Clone this git repository
- Install the .oxt extension (as above)
- Open Writer and go to
Tools -> Macros -> Organize Macros -> BASIC - Find the macro text file under
My Macros / Wordcounter / wordcount_sectionsthen clickEdit
Once you have finished changing the source code, turn the macro back into an extension:
- Update the library version in
description.xml: line<version value="X.Y"/> - First export the macro as a library:
- Go to
Tools -> Macros -> Organize Macros -> BASIC - Click
OrganizerandLibrariestab - Browse to the
Wordcountermacro and click theExportbutton - Select
Export as BASIC Library - In the
Savedialogue, save the files into the directory where you cloned this git repository. This will replace the source files in theWordcountersubdirectory.
- Go to
- From the git clone root, type
maketo zip the changed files into an .oxt extension file. - To tag a new version, commit, then
git tag -n,git tag vX.Y -am "Name of tag",git push origin vX.Y - Now submit the extension on the LibreOffice Extensions website. Select the MIT license.