UPDATE 2: LibreOffice Extension
I have published my previous macro script as an extension: Code Highlighter which is available in LibreOffice Extensions store.
1. How to install?
Follow these steps to install the extension:
Step 1: Install libreoffice-script-provider-python:
sudo apt-get install libreoffice-script-provider-python
Step 2: Install pygments for Python 3:
sudo pip3 install pygments
Step 3: Download and add the codehighlighter.oxt extension to Libre Office
2. How to use?
- Open LibreOffice Writer/Calc/Impress.
- Insert a Text Box (Insert -> Text Box).
- Copy and paste any code snippet into that Text Box.
- Select the Text Box.
- Highlight by Tools -> Highlight Code -> < Language-Name >

You can assign keyboard shortcuts for frequently used languages. To see how to assign keyboard shortcut, visit the Git Hub page: libreoffice-code-highlighter
I leave my original answer here since it is still valid for LO <= 5.
I had the same requirement and none of these solutions worked for me in LibreOffice 5 except 'Code Colorizer Formatter'. But Code Colorizer Formatter worked in Writer only (I also needed for Impress). Later I found another solution using Python macro which is tested in LibreOffice 5.
Installation
Step 1: Install libreoffice-script-provider-python:
sudo apt-get install libreoffice-script-provider-python
Step 2: Install pygments for Python 3:
sudo pip3 install pygments
Step 3: Download Highlight.py
Step 4: Copy the Highlight.py to /usr/lib/libreoffice/share/Scripts/python/
sudo cp Highlight.py /usr/lib/libreoffice/share/Scripts/python/
Step 5: Set the Read-Only permission to the script.
sudo chmod 0444 /usr/lib/libreoffice/share/Scripts/python/Highlight.py
Step 6: Open the LibreOffice Writer and goto Tools -> Customize -> Keyboard
- Select the LibreOffice option button (Available on top left corner)
- Select any desired shortcut
- Select share/Highlight/Highlight_source_code under the Functions Category
- Click on the Modify button to set the shortcut
Usage
LibreOffice Impress
- Open LibreOffice Impress.
- Insert a Text Box (Insert -> Text Box).
- Copy and paste any code snippet into that Text Box.
- Create a new Style (Right click on an existing style -> New...)
- Name the Style as
code-<language>
- Apply the style to your code snippet Text Box.
- Select the Text Box.
- Use your shortcut keys to execute the macro.
LibreOffice Writer/Calc
- Open LibreOffice Writer/Calc.
- Insert a Text Box (Insert -> Text Box).
- Copy and paste any code snippet into that Text Box.
- Right click on the Text Box and select 'Description'.
- Add a description in the following format:
code-<language>
- Select the Text Box
- Use your shortcut keys to execute the macro.
UPDATE 1:
I have added custom style support to this extension. Use code-<language>
for default style and code-<language>-<style>
for custom style. Available styles are listed in my Git Hub page.
For example, Code-Java-Emacs
will color the Java code using Emacs style.