18

I need to use MS Word to make some documentation for Python code. Line number, or boxing around the code, or anything would be better than just use normal format.

Is there any good/useful style/method for code documentation purposes?

phuclv
  • 30,396
  • 15
  • 136
  • 260
prosseek
  • 6,054

6 Answers6

11

At the most basic you can select a monospaced font - usually Courier New - as your code font. You won't get all the syntax highlighting, but it will stand out from the rest of your document.

ChrisF
  • 41,540
7

Do you have an editor that formats python code for you? I ask because Visual Studio will copy code onto the clipboard with formatting intact so that when pasted into word it looks identical (give or take line lengths).

Visual Studio itself supports python now. The Community Edition doesn't have the somewhat restrictive licensing limitations of the old Express edition; meaning it's likely to be an - admittedly heavyweight - option to get formatted code into other documents even if your preferred editor doesn't put syntax highlighted code onto the clipboard.

I've added an image of what pasting phuclv's sample program from VS 2022 (17.7.3) into Word 2010 looks like. Syntax highlighting is identical to what Visual Studio's defaults. All the spelling grammar check red/green squiggles are annoying, but can either be ignored or the checks disabled for the document as a whole.

Example of Python code copied from Visual Studio to MS Word with syntax highlighting

2

You need to add Office-addin called Easy Syntax Highlighter

from Insert tab click ... > Add-ins >

search for Easy Syntax Highlighter > Add

It will show as a new tab

Setting > choose your prefered Theme

Personally I like Solarized Light

tijko
  • 356
1

If you manage to copy code style from your IDE, you'll find that the font used for Eclipse IDE (for example) is "Consolas". I find that it looks pretty good in MS Word.

If you require 100% of the formatting you might be able to find an online tool that will do the highlighting for you.

ChrisF
  • 41,540
1

In VS Code just press Ctrl+Shift+P then search for the "Copy With Syntax Highlighting" command

VS Code copy with syntax highlighting

In fact you don't even to need to use that feature, because copy with formatting is enabled by default

VS Code Copy With Formatting Settings

phuclv
  • 30,396
  • 15
  • 136
  • 260
0

If you have Notepad++ then just select the code then select menu Plugins > NppExport > Copy RTF to clipboard and paste to Word

Notepad copy with format

phuclv
  • 30,396
  • 15
  • 136
  • 260