How to leave code in language format within word

Asked

Viewed 9,018 times

2

I currently have a code in the C language. I need to present this code in a PDF or .doc. But formatting a code in Microsoft Word to make it equal in the IDE is very laborious.

Is there any tool that I can export code from the IDE directly to Word?

  • Using Internet Explorer on the site below gets a good format, but I do not know any automatic tool. http://www.planetb.ca/syntax-highlight-word

  • 2

    When you copy from the IDE and paste into MS Word does not preserve formatting? I don’t know which IDE you use, but I already did it with C# code in Visual Studio, it preserved the formatting by pasting it into some word processor.

  • Which IDE you are using?

  • I use Devc++. At least save as you have no option

1 answer

4

It has an interesting path, using unusual tools:

The Python language follows one of the principles of the Unix philosophy where it is better to have several small tools that do one thing and do one thing well.

Then, shared among the various interactive interpreters, Ides, template Engines, there is a package in Python that does the color formatting of language source code, the package pygments. And the interesting thing is that it both accepts several input languages - over 300, and of course c including, as can be used directly as a command line tool, without needing a Python program to be used. It various output formats, with color formatting. Here tested quickly: it has no output in .docx, but has output in .rtf that Word should read with colors and tabs in place, no problem. (Can also create files png or others with images of the programs).

Windows

How do I do if you don’t have a Python environment? Well, create one - the entire Python Runtime is very light and quick to install - go to the site http://python.org and download the latest version (at this time, the 3.6, in a few months to 3.7). I believe that during the installation there is an option to "ensure that Python is in the Path" - enable this option.

Python is usually installed in C:\Python36\bin\Python - you will not need to type the directory in the next commands if you added Python to your Path. Try typing python<enter> cmd, and see if it goes inside Python (the prompt is >>>), or if you need to enter the directory and which directory. When it works, exit by typing ctrl + Z and <enter>.

Now just install Pygments - for that, type in cmd prompt: python -m pip install pygments

This will install pygments, which can either be used as a Python library, or provides a command line tool to create the color output in various formats (ANSI terminal, .rtf, .html, etc...) of the color source code.

Go with the command cd to the folder where your C code is and type something like: c:\python36\scripts\pygmentize -o arquivo.rtf arquivo.c - this should create a new file of type "rtf" (which is a "light" format of formatted text that Microsoft created with Wordpad that came for free with windows since the 90s. ) If you don’t have an error message, you should have the file. rtf in the same folder, just open it with Word and paste into your final document (or use options formatar->inserir, etc....).

If you have any error in the process will be relative to the installation folders and paths in windows - just figure out the correct paths.

Linux, Mac OS

On other systems, Python is already installed, and pygments exists as a system application - something like apt-get install python-pygments or pip install pygments direct on Mac, and you will have the command pygmentize at your disposal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.