Syntax Highlighting in Microsoft Word

If you’ve ever tried copy-pasting source codes into a Word document, you know what a nightmare it is to apply syntax highlighting. Most probably, you have to manually apply colors on sections of source code. While searching for a solution, I came to know about a Notepad++ plugin NppExport that’ll help you copy code from Notepad++ and paste it in Microsoft Word with syntax-coloring attached.

If you haven’t got a copy of Notepad++, download it from their download page. After installing it on your machine, launch Notepad++ and copy-paste this Python source code into a new file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# This code snippet is a part of the Planet Planet feed aggregator

if feed_timeout:
        try:
            feed_timeout = float(feed_timeout)
        except:
            log.warning("Feed timeout set to invalid value '%s', skipping", feed_timeout)
            feed_timeout = None

    if feed_timeout and not offline:
        try:
            from planet import timeoutsocket
            timeoutsocket.setDefaultSocketTimeout(feed_timeout)
            log.debug("Socket timeout set to %d seconds", feed_timeout)
        except ImportError:
            import socket
            if hasattr(socket, 'setdefaulttimeout'):
                log.debug("timeoutsocket not found, using python function")
                socket.setdefaulttimeout(feed_timeout)
                log.debug("Socket timeout set to %d seconds", feed_timeout)
            else:
                log.error("Unable to set timeout to %d seconds", feed_timeout)

Now go to Language > P > Python to apply syntax highlighting for Python code. You can select other languages depending on your requirements. You’ll notice that the source code is now colorful. Now go to Plugins > NPPExport > Copy all formats to clipboard. Selecting this option saves a copy of the selected source code in the clipboard. Launch Microsoft Word and press Ctrl + V to paste the copied source code. Here is how it looks like in Microsoft Word:

If you’re aware of any other method, do let me know in your comments.

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
Syntax Highlighting in Microsoft Word, 10.0 out of 10 based on 1 rating

No comments.

Leave a Reply

healthlibr.com