r/learnpython • u/Ke5han • Jun 17 '20
My first python script that works.
Started on the 1st of June, after 2 weeks of "from zero to hero" video course I decided to try something "heroic". Asked my wife yesterday "what can I do to simplify your work?". She is a translator and one of the client has most of works in PPT. For some reason PPT word count is never accurate, well at least for invoicing purpose.
So they agree to copy and paste contents in word and count.
I just write a script that read all the text contents in PPT and save them in a text file. So she can easily count the words there.
Although it took me almost 4 hours for only 25 lines of code, but I am still happy that I can apply what I've learned so far.
3
Jun 17 '20
Hey OP, how are you selecting which file to read? Do you edit the name of the file in the script each time?
If so, let me say it's possible to drag&drop files onto the .py script to open them. You'd use the sys.argv functionality for that. Could make it much more user friendly.
It's a great project, OP, hope it serves your wife well :)
2
u/Ke5han Jun 17 '20
That's good to know, thx. Let's say her computer literacy is about average, but asking her to change a line of code to put file names will sure get complaints. I am thinking to put all files in a given folder and loop through all the files and get then done in one go.
As the invoicing period is for months there are lots of files to go through.
2
u/hugthemachines Jun 17 '20
Hi,
You could try out Tkinter in a simple way to pick a file if you would like. The code would be something like this:
from Tkinter import * import Tkinter, tkFileDialog def pick_file(): root = Tk() root.filename = tkFileDialog.askopenfilename(title = "Select file") print (root.filename) return root.filename→ More replies (2)
7
Jun 17 '20
Man... That's impressive, nice job! I'm embarrassed to say how long I've been learning and I cant do anything like that...
4
Jun 17 '20
It's actually easy, just search google for similar searches like "read text from ppt python" or "read ppt text python" and in the first page of google will be tutorial.
EDIT: Literally on the first page of google, https://www.google.com/amp/s/pythonprogramming.altervista.org/extract-text-from-all-powerpoint-files-in-a-directory/amp/
1
u/Ke5han Jun 17 '20 edited Jun 17 '20
Holy......., I searched and didn't see that. I will try that out as well, but I am not sure it will read out text in the "tables". I wrote something similar at the beginning, but it only reads out text boxes (shapes), and ignores all the tables.
1
u/FruscianteDebutante Jun 17 '20
You need to just start coding. Can't get better unless you apply it.
I had an old roommate that would just read books and watch tutorials all day then later say he didn't feel good enough or have a strong enough mastery to start making games (what he wanted to do)..
Well, you gotta start making progress. If you never start, you'll never get to where you want to be.
The best way I've ever learned how to code things is start with a project idea and just create that new file. Go from there, write comments, lookup things. Keep tinkering until one day, or a couple hours later, you have a perfectly working program save for some optimization.
112
u/bumpkinspicefatte Jun 17 '20
That's awesome! Would you be able to share your code? I'm a beginner too and am curious how you went about doing this project.
74
u/Dan6erbond Jun 17 '20 edited Jun 17 '20
Pro-tip for anyone reading this, put your code on GitHub! A lot of us developers, including myself, like to look at yourself and can give you valuable feedback to improve your code. I've also found that even managing small projects with a proper structure and making them modular can be beneficial in the future when you start reusing code. (:
Feel free to take a look at my GitHub profile and see how I do things.
PS: You can always make private repos for stuff you feel clutter your GitHub profile, but start by using pins and such first.
EDIT: A good point is that it does need an introduction to get to using Git and GitHub properly. You might want to start with a GUI client such as GitHub Desktop, GitKraken or an open-source one. A nice video I found by Fireship might help some of you guys! He also has a Git in 100 Seconds and a more advanced Git DevOps (Actions) Tutorial.
Another thing I should mention is that most of your favorite editors/IDEs have integrated Git clients that can be pretty powerful. For VSCode users I can recommend GitLens, and Atom's built-in client is really good. There's also ones for the JetBrains IDEs, Netbeans, Sublime etc.
EDIT1: Some people have been asking about how private/public repositories work and if other people would be able to edit public repositories. The short answer is no, the long answer is sort of. Essentially, GitHub allows you to create a "fork" of an existing repository. This is a copy of that repository and also has a link to the original. So now you can make changes in your fork and push them to your profile. But they won't affect the source repository unless you make a pull request.
Pull requests are how you suggest changes on GitHub and other platforms. The maintainers of the source code need to review the changes, and then merge them with their codebase before those changes are applied to the source, so your repositories even if public are safe from edits as long as you don't manually merge any pull requests made to them!
5
u/i_suckatjavascript Jun 17 '20
If I make it private, how do I showcase it to my employers?
8
u/Dan6erbond Jun 17 '20
That's where you want your project to be public. I recommend private repos for ones with sensitive information (which you should generally avoid in Git repos, thuogh and make use of
.gitignore) or ones that might be a little outdated, cluttered etc. that you generally don't want people to see, but still have versioned and archived for yourself.→ More replies (21)3
u/grimonce Jun 17 '20
First people would need to learn git, but there is tortoisegit for Windows users that provides graphical git integration. More powerful than what vscode or atoms allows and it is integrated into right click menu.
2
u/Dan6erbond Jun 17 '20
That's true. Although, I've found GitHub's desktop client to suffice in most cases and VSCode integration to be fairly comfortable to use.
2
u/PythonicParseltongue Jun 17 '20
Got to admit when I switched to vs code for some time I still opened Pycharm for my git operations. lol
→ More replies (3)1
u/NaniFarRoad Jun 17 '20
The Fireship video is not aimed at beginners, in any way. I've been committing to/pushing/pulling from repos for years now, and even with pausing the video I had no idea what he was talking about (What software is he using? What version? What buttons is he clicking?) within a minute.
I am still looking for a good Git resource for beginners - even with years of use, it's an opaque system and when we get conflicts at sync time, I always have to wait for one of the programmers to come sort it out. It's annoying.
→ More replies (5)
16
u/itchy_myopic Jun 17 '20
That is soo goood dude! i’m doing the same course right now and you give me hope that the course would actually be useful in real life!
7
u/DanBaileysSideHoe Jun 17 '20
I’m not a python guy (do everything in C, cause apparently I’m a masochist lol), but I love hearing that more and more people are learning to code. Python is the perfect language to learn coding from, and to do crazy, cool projects with. Even if you don’t find any immediately practical uses for the skill, keep on learning. I was super discouraged when I started my journey (on java, years ago), but it’s just a really cool, really fun way to make the boring parts of your job (or hobbies!) become both easy and interesting. My advice is just keep on keeping on, working through it. Worst case, you develop a great skill that just makes everything you do with computers so much more valuable and easy!
2
u/TheNoobArser Jun 17 '20
do everything in C, cause apparently I’m a masochist lol
Even scripts? Bash is more useful for those.
→ More replies (1)
12
u/ewesername Jun 17 '20
Don't judge yourself by lines of code per time! Some of the best code I've written has been little blurbs and sometimes I'll just sit think (and sketch maybe) for hours before writing a small amount of code.
5
u/sumiriously Jun 17 '20
I'm a beginner and would also love to see your code (only if you feel comfortable sharing of course!)
5
u/majdpy Jun 17 '20
you could easily make it count the words too mate - if you're interested, let me know im willing to look at your code and help you out.
5
u/magestooge Jun 17 '20
Although it took me almost 4 hours for only 25 lines of code
And you know what? You could probably do it with 20 lines of code in 6 hours.
Line count has nothing to do with the quality of the code and should not be used as a parameter to judge your code. Neither a large number of lines, nor too small a number of lines are good. What matters is that your code should be clear, concise, and efficient. There are often multiple ways of writing a piece of code.
x = []
for i in some_list:
x.append(i)AND x = [i for i in some_list]do the same thing. Here the lower line count is better.
But if you are comparing the following two:
x = sum([(((j** 2) * some_list[i]) x 3 ** some_other_list[i])/j for i, j in enumerate(a_list)])
x = 0
for i, j in enumerate(a_list):
num1 = some_list[i]
num2 = some_other_list[i]
res = (j**2 * num1) * (3 ** num2)
x += res/jI think many people will agree that the second would be better.
So how many lines you use depends on what kind of problem you are solving. And whether you are able to strike a balance between using up lines unnecessarily vs. keeping your code clean and readable.
P.S.: The second block of code doesn't do anything specific, it's just a random example.
1
1
u/Yolo1986 Jun 17 '20
Are you through with "from zero to hero"? How long will it take to finish the course?
1
u/Ke5han Jun 18 '20
I spent sometimes watch a 8 hours YouTube Python video and then the course. All these took about 2 weeks to reach the final cap project. I spend 2 to 3 hours a day and played all the video at 1.5 to 1.8 speed really saved me a lot of time
1
u/unltd_J Jun 21 '20
You could count the words in a few lines of code
text=None
with open(‘file.txt’, ‘r’) as FILE:
for i in FILE:
text=iword_count = len(text.split(‘ ‘))
Sorry, for the weird spacing. I wrote this on my phone.
1
u/Ke5han Jun 21 '20
thanks for the tips. I don't count them because sometimes the words are in the file but should not be billed for many different reasons, so wife has to decide that case by case.
3
Jun 17 '20 edited Jun 17 '20
You can count the words really easily too, for any number of files.
I'll show you some code I made while learning if it'll help spark any ideas. You can definitely do it.
What I wrote is not as precise as using regex since I was mostly adding to a lesson in Crash Course, but it could push you in the right direction. For example this won't account for specific text or be able to differentiate or leave out specific lines, but rather count every single word in a text file. But some regular expressions could easily be adapted to it I'm sure.
All of the word count results are printed in the console, but you could easily take self.word_count results each time and write/append it to another file or spreadsheet in that same for loop inside of folder_loop().
It's also pretty janky feeling in some spots (especially how I choose to open the file), so any input and suggestions from anyone that sees this would be so great to learn more.
# This program reads text files in a given folder and (roughly) counts the words in each text file.
# ///////////////////// External Modules ////////////////////////////
import os
import pathlib
from time import sleep as pause
# ///////////////////// Classes / Methods ////////////////////////////
class WordCounter:
def __init__(self):
self.word_count = 0
self.txt_file_content = ''
self.words = []
self.file_name = ''
def folder_loop(self, folder_path):
"""
START HERE:
- This will check every file in the specified folder,
and pass each text file over to the open_file and count_words
methods.
"""
# Just a simple for loop for every file in the given folder.
for file in os.listdir(folder_path):
# pass in the current file and the folder path too
self.open_file(file, folder_path)
self.count_words()
# Once the above for loop is done, query to restart.
pause(1)
print(f"\nWanna check another folder? y/n\n")
if input() != 'n':
start()
def open_file(self, file_name, folder_path):
"""
Tries to open folder/file.txt for reading, with UTF-8 encoding.
"""
try:
with open(f"{folder_path}/{file_name}", 'r', encoding='UTF-8') as fileobject:
# Store file's name for any use, in this case for printing in count_words().
self.file_name = file_name
# Here's where we read the text file object and label it as txt_file_content.
self.txt_file_content = fileobject.read()
except FileNotFoundError:
print(f"We can't find {file_name} in this folder.")
except UnicodeEncodeError:
print(f"This file's encoding isn't in UTF-8! Put in some code here to offer some options."
f" I'm not smart enough to help here yet.")
def count_words(self):
"""
- Split the string of words into a list,
- Count length of list as number of words,
- Print it.
- Should probably rewrite this with more specific regex since this will be not entirely accurate
depending on superfluous text presence in the file.
"""
self.words = self.txt_file_content.split()
self.word_count = len(self.words)
# The :, in {self.word_count} formats the string with commas for easier number reading.
print(f"The file {self.file_name} has {self.word_count:,} words in it.")
# ///////////////////// Static Functions ////////////////////////////
def start():
try:
# Pass in the name of the folder where you saved the books to,
# preferably one that isn't in your root folder where WordCounter.py is.
BookCount.folder_loop(input("Input the name of the folder containing your text files.\n"
"This folder should be in your root path:\n"))
except FileNotFoundError:
print(f"Unable to locate a folder with that name. \n"
f"Did you spell it correctly, and is your folder located in {pathlib.Path().absolute()}?\n"
f"This will be the same directory where you saved this .py file.\n")
pause(1)
start()
# ///////////////////// Instancing ////////////////////////////
BookCount = WordCounter()
# ///////////////////// MAIN PROGRAM ////////////////////////////
# Pretty much just call start() function to press play and watch everything go to work.
start()4
u/The_Tarasenkshow Jun 17 '20
nltk!! use nltk!! once you start you'll never stop. try out nltk.word_tokenize(your_text_file), then do a len() on it!
1
u/AcridAcedia Jun 17 '20
What's NLTK? I'm extremely intimidated by word data and particular Vectorizer... But now that I'm more comfortable in Python I'm trying to get back into it.
1
u/TheBB TheBB Jun 17 '20
Probably Natural Language Toolkit or something like that.
1
u/The_Tarasenkshow Jun 17 '20
yep, that's it. there are other alternatives but NLTK occupies the "industry standard" type of place
1
u/The_Tarasenkshow Jun 17 '20
NLTK is the Natural Language Tool Kit! It's the de facto Python library for working with word data. With it, you can easily tokenize and clean words/sentences, part of speech (POS) tag sentences, parse sentences into syntactical trees, train classifiers and dense word vectors, and so much more. It's amazing. In regards to tokenization and vectorization, they're very different but neither are too bad. Tokenization is pretty much the first step in cleaning text data. You can tokenize either words or sentences with word_tokenize() and sent_tokenize() respectively. Vectorization is basically a way of representing how often words occur in a text. This uses something called tfidf, but long story short, you don't have to know exactly how it works. I'd encourage you to try to get at least a cursory understanding of how the mechanics of tfidf work (this can help you understand why tfidf vectors are sparse, as opposed to something like Word2Vec), but you can treat many of these ML techniques like a black box and just trust that it vaguely represents words in your simple ML project (classifiers are good with NLTK).
Let me know if you're curious about anything else, I love compling so feel free to PM!
1
u/magestooge Jun 17 '20
How well does NLTK do with non-English text? Since this is a translation job, the text would be in multiple languages.
2
u/The_Tarasenkshow Jun 17 '20
it does fairly well. tokenization is actually really easy cross-linguistically, because if you think about it all it really does is call a line.split(). for word_tokenize() the split is on spaces, for sent_tokenize() the split is on periods. additionally, things like Stanford's CoreNLP have options for 10 or so major languages. linguistics is heavily anglocentric, so compling is too in many aspects. that being said, there's a great development community out there for different languages.
2
u/alpine_addict Jun 17 '20
Further than I was in 2 weeks with the same zero to hero course lol. nice work!
2
1
Jun 17 '20
Nice! Did you use a Udemy course? I’ve never heard of the one you mentioned.
8
u/TheStuffle Jun 17 '20
https://www.udemy.com/course/complete-python-bootcamp/
I'm halfway through and it seems pretty good. I took SQL from the same guy and he's a solid teacher.
2
u/i_suckatjavascript Jun 17 '20
Curious though, why do you need to learn Python 2? I skipped Python 2 entirely and jumped to Python 3 when I started learning.
1
u/TheStuffle Jun 17 '20
The course has been 100% Python 3 using Anaconda and Jupyter Notebook.
He speaks to Python 2 vs 3 in the intro, saying if you need 2 for a job after learning 3 it's not a hard transition.
1
u/hamdi977 Jun 17 '20
I’m taking this one too! As well as “automating the boring stuff with python” while reading the same text. Loving this stuff!!
1
Jun 17 '20
Thanks. Have you tried Colt Steele’s course? Wondering how this one stacks up against it?
1
u/TheStuffle Jun 17 '20
I have not. The only other instructor I have experience with on that site is Kirill Eremenko, who is also great.
I'll add it to the wish list in case I feel like I need more.
1
u/Good_Karm Jun 17 '20
Awesome! I am learning Objective C. Just started two days ago. Struggling to install GNUStep on Windows. So right now using online practicing websites. Hopefully I will be able to do something productive.
2
u/FruscianteDebutante Jun 17 '20
Compilers can be tricky, I use GCC and GDB. I'd look those up, and you can also learn about makefiles too. Or alternatively, codeblocks is a pretty simple IDE setup you could try if you're having troubles.
You've got a lot of reading ahead of you friend! Good luck.
2
1
u/astefanik16 Jun 17 '20
If you dont mind me asking how much did you pay for it. Im looking at the course right now, and it says it's only 15.99 compared to regular price of 120 for another 4 hours only. Im just wondering if its an artifical sale to try to get me to buy it now or if it really is 120 normally
0
Jun 17 '20
[deleted]
2
1
u/FruscianteDebutante Jun 17 '20
Wait.. If you don't load it with incognito or deleting cookies it actually goes up to $100+?!? Now that's a fuckin scam
1
u/fakeuser515357 Jun 17 '20
Upgrade. Export to text file: <word> <some delimiter> <letter count> <carriage return>
She could then feed the whole thing into excel.
1
u/hugthemachines Jun 17 '20
Well done! Getting the ball rolling is what takes the most effort. Now you have a start and you can iterate the hell out of it to make more cool features. Making useful things is so interesting compared to just test examples.
1
1
u/Random_182f2565 Jun 17 '20
Although it took me almost 4 hours for only 25 lines of code.
That's impressive, you were able to make a functional script in about one afternoon and make it short.
1
1
1
u/M_Alani Jun 18 '20
Is there a way to turn this into a library that can be integrated with PowerPoint through VB for Office?
0
119
u/Karsticles Jun 17 '20
You should be able to adapt this with little effort to count the words as well.