'How to write code to MS Word using python retaining the formatting?
I want to create a MS word document which compiles a lot of my existing codes ( in MATLAB and Python). I was writing it using python-docx.
If I do something like:
file = open('task1.m', 'r')
document.add_paragraph(file)
Then the code gets written in MS word in simple text format without the formatting.
Is there some way I can write the code while retaining the programming language formatting? (Keeping the colors intact)
Solution 1:[1]
For a quick and dirty way to achieve this, NotePad++ has a feature where you can turn on syntax highlighting for your language. Then, select the code, right-click and select "Plugin Commands > Copy Text with Syntax Highlighting". Now, you can paste that into Word and the colors remain.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Brian.Hoard |