How to leave code in the format of the language inside the word [closed]

2

I currently have a code in C language. I need to present this code in a job through PDF or .doc. But formatting a code in Microsoft Word to make it the same in the IDE is a lot of work.

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

    
asked by anonymous 19.12.2017 / 00:45

1 answer

4

It has an interesting way, using unusual tools:

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

So, shared between the various interactive interpreters, IDEs, template engines, there is a package in Python that formats language source code, the package pygments . And the interesting thing is that both it accepts multiple input languages - more than 300, and of course c inclusive, as it can be used directly as a command line tool, without needing a program in Python to be used. It output multiple formats, with color formatting. Here I quickly tested: it does not have output in .docx , but has output in .rtf that Word should read with the colors and tabs in place, without problems. (You can also create png or other files with program images).

Windows

How do you do if you do not have a Python environment? Well, create one - the whole Python runtime is lightweight and fast to install - go to link and download the latest version (at that time, the 3.6, in some months to 3.7). I believe that during the installation there is an option to "ensure that Python is in Path" - enable this option.

Normally Python is 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 to type python<enter> in 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, quit typing ctrl + Z and <enter> .

Now just install Pygments - for this, type at the cmd prompt: python -m pip install pygments

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

Go with the command cd to the folder where your code is in C 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 formatted text "light" format that Microsoft created with Wordpad that came with Windows for free since the 90's.) If you do not have an error message, you should have the .rtf file in the same folder, just open it with Word and paste it in the your final document (or use options of formatar->inserir , etc ...).

If you have any errors in the process it will be relative to folders and installation paths in windows - just find 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 the Mac, and you have the command pygmentize available.

    
20.12.2017 / 03:16