Convert numbers to words
Ever had a spreadsheet full of numbers during a deal, then realised you need to insert the consideration in both numbers and words?
This is a total drag, which serves (I assume) to ensure you are filling in the numbers correctly. Since some fancy corporate financier has probably generated the numbers in Excel, really the only human error likely to occur is in the intercession of actually typing up the numbers.
To remedy this, we can use a simple program that will spit the word-numbers out based on the numeric input. This may sound tricky, and it probably is. Fortunately, some useful soul has done the work for us and made it available.
- Install the num2words module
- Prepare our data
- Use the module
1. Install our module
We only need to install one module, which has all the functionality we need for our task! The module we will be using is called 'num2words
If you are unsure how to install this module, check the guide at python basics.
2. Prepare our data
While python gives us lots of ways to deal with spreadsheet input and output easily, that's covered in a different snippet. In this case, we'll take the easy approach and paste the data into a text file.
Create a new folder. Let's call it 'numwords'. Either do it in your usual way, or in the terminal enter:
$ mkdir numwords
In the new folder, create a text file (use Notepad, Kate, Gedit... any text editor will do, but not a word processor like Microsoft Word or LibreOffice - check the glossary if you are not sure about the distinction.
Paste in your numbers, with each one in a different row of the text file.
If you're just learning and don't have a real deal on your hands, just enter some random numbers.
Save your file as 'numbers.txt'.
3. Use the module.
Let's write a script!
In the 'numwords' folder, open a new file called 'numwords.py'.
Enter the following script:
Now, run the script. You may see the program spit out some numbers. Check the folder and we'll see a new file, with the output neatly stacked and ready for copy-pasting back into your spreadsheet. Wow!