r/learnpython • • Oct 18 '20

I still don’t fully understand how automation works

So, I do know some of the basics of automation. I’ve watched the automation section on automate the boring stuff with python and I’m currently watching a video talking about automating with Selenium. But... There are still some things I don’t get. Like, how do you actually automate stuff like excel, word, and other applications, Do you always have to go to the terminal to run an automation program or do you have to create an application on the desktop, are there certain things I have to download, is it possible to automate someone else’s computer from your computer, etc etc.

I’m sorry if these questions seem basic to some of you but I’m genuinely confused on this

367 Upvotes

67 comments sorted by

119

u/poppycocknbalderdash Oct 18 '20

There’s loads of different ways really, but they all essentially come down to writing a script to do X then running that script somewhere. E.g. a script that plays an alarm every hour. You could put this into your boot up files on a computer to set the script running every time it turns on, then in the script you’d have some code that checks the time (while loop), when it’s o’clock ring the alarm. Similarly, this could be hosted in the cloud somewhere then sends a command to a different computer to sound the alarm.

This is a basic example but essentially the script itself usually contains the thing you want to automate, as well as a check for the thing that initiates it.

21

u/Ddog135 Oct 18 '20

Seems simple enough to be a starting project for me to try out. After reading this along with the other comments, I think I’ve got a better understanding of all this now. (Just need to try it myself)

19

u/mmicoandthegirl Oct 18 '20

For example I'm automating sending reminder for late payments. Check folder for invoices -> copy those invoices in a dated new folder -> create reminders for every invoice in folder -> for every invoice in folder send an email reminder to the email address provided in the invoice. You don't automate excel or word or anything, you automate tasks that might use those programs.

5

u/rpo5015 Oct 18 '20

When I started out automating things I started tackling things I had to do each day that would be repeatable and would take me an hour or so but might be something I could knock out with a local script like parsing alerts or grabbing traffic stats from the firewalls and writing it to a CSV. You kind of just start doing simple things like that and bam all of a sudden you have a reporting tool you wrote and have been expanding on

1

u/luckiest0522 Oct 19 '20

Could you clarify the cloud piece? Maybe a guide for it? How do I put a standalone script in the cloud and get it to run?

5

u/Moccar Oct 19 '20

On the different cloud platforms you could use:

AWS Lambda (https://aws.amazon.com/lambda/)

Azure Functions (https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview)
Azure Logic Apps (https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-overview)

GCE Functions (https://cloud.google.com/functions)

They can be set to run on a schedule or a trigger. Look into the documentation on the platform you want to use and it should be fairly straight forward :-).

2

u/luckiest0522 Oct 19 '20

Thank you! Would you say this requires an intermediate level of understanding? Can a beginner do this too?

1

u/Moccar Oct 19 '20

I would say it takes a couple of hours to get into but there is loads of documentation, tutorials and discussions on all of them as they are used by quite a lot of businesses worldwide. But overall a beginner could definitely start trying this. Logic apps are heavily GUI based, like building blocks, but can be quite advanced and include Azure Functions on top of them.

If you do not have a job in this area, this is definitely something you also can add to your CV that is much sought after in the industry.

41

u/rotterdamn8 Oct 18 '20

There's automation outside of Windows as well. Like Unix and Linux have cron jobs. Or, SQL Server and other reporting tools allow you to schedule reports to run.

19

u/MethodicOwl45 Oct 18 '20

Windows equivalent would be the Scheduler

5

u/Ddog135 Oct 18 '20

What about for Mac?

9

u/Wicht1 Oct 18 '20

Cronjob aswell. Its basically linux

3

u/Ddog135 Oct 18 '20

Ah thank you (still kind of unfamiliar with some stuff like this)

2

u/Giannie Oct 18 '20

It’s definitely not Linux, since is doesn’t use the Linux kernel. It is posix though. Which is what you’re thinking of. Linux is not an operating system, but rather a kernel and macOS uses the Darwin kernel.

1

u/MethodicOwl45 Oct 18 '20

Actually didn't realise this. I always forget that it's a freebsd variant

3

u/Wilfred-kun so much to learn Oct 18 '20

Mac has cron too.

22

u/Nixellion Oct 18 '20

Task Scheduler is the full name

10

u/MethodicOwl45 Oct 18 '20

Thank you kindly

6

u/Jonno_FTW jonno_ftw Oct 18 '20

Advanced Python Scheduler (APS) is a python version. https://apscheduler.readthedocs.io/en/stable/

You will still need to start the python process in the first place though.

1

u/Nixellion Oct 19 '20

Not a replacement, really. Task Scheduler and Cron are OS built in systems. Basically you will still use Task Scheduler to start APS in the first place.

Task Scheduler also has triggers for pc idle, log on, boot and other stuff. Cron too, though I feel like TS in Windows is even more flexible

3

u/[deleted] Oct 18 '20

I struggled with automation too but I started creating projects that helped me understand it. Even though I am not a master, I created some projects that taught me a lot. Just keep doing projects. That is one of the best ways to learn any language, frameworks and libraries. If you want some simple automation projects, you can check out my Github. Even though my projects are not the best or the most advanced and has some bugs, but they did help some other people. I hope this helps you too.

2

u/Ddog135 Oct 18 '20

I appreciate the fact they’re not advanced considering I’m a beginner lol. These seem simple enough to really help me out with understanding all this

70

u/nomowolf (pandas) Oct 18 '20 edited Oct 18 '20

Simple real-world example for ya:

Problem: Guy in work (former office) spent hours every morning manually checking for any new data from our products at customers and downloading it. Only in the afternoon could he analyze and report on it (the fun part for him).

Solution:

  • I wrote a simple python script that checks what data is new, and downloads it only if we don't have it already.

  • A one-liner batch script to call the script in windows (literally just run.bat with python downloader.py written in it).

  • Put the 2x simple files on a work network PC that was always turned on.

  • Used the built-in Windows Task Scheduler to schedule to run this every morning a 4:00am. (If you're using Windows Home say and don't have task scheduler there are plenty alteratives).

Result: Dude was delighted. Said I gave him his mornings back... he could get his report out in the morning now and spend the afternoon pursuing other ambitions. Helped the company too because issues were reported early and people had the whole rest of the day to work on them. People had really hated hearing about emergency product issues just at the end of the day!

.

Edit: (quick addendum!) Later I started writing scripts to do his analysis (with permission, don't wanna step on anyone's toes), create the report, and email it daily to everyone... you can keep going deeper!

6

u/AceBuddy Oct 18 '20

This is most of my motivation to learn, I’ve probably saved 30% of my day just by automating simple tasks. It’s really awesome because not only is it more reliable but I don’t have to do Jack squat once I’ve written the program. Really a career booster for me anyways.

5

u/PeopleCallMeBarry Oct 18 '20

That's really cool and similar to how I would like to implement some automation. Thanks for sharing! - Also thank you for making me aware of Task Scheduler, that's going to allow me to do all sorts of things!

4

u/Working-Mind Oct 18 '20

I love real life examples such as this. Thank you for sharing

3

u/ouellp Oct 19 '20

FYI, you can launch python.exe and add your script as an argument from Windows task scheduler. It's a bit simpler.

2

u/nomowolf (pandas) Oct 19 '20

Hah nice one :)

17

u/[deleted] Oct 18 '20

[removed] — view removed comment

27

u/nomowolf (pandas) Oct 18 '20 edited Oct 18 '20

I only discovered that book after learning so never read through it. My start was with coursera courses (with a slant on datascience).

Looking briefly at the contents though, seems to cover basics (datatypes, functions) and then goes over using it to interact (files, documents, emails). So I would say yes, with a caveat.

You're always and constantly going to need to google stuff while coding. You basically won't be able to do almost anything fully out of the box ever, and that's OK... cos you don't need to. Programmers make jokes about this all the time. Of course being 50%-able to do something makes it a easier to know what to google to figure out the missing bits :)

.

Example: Pandas is the library (collection of functions other people made) I mainly use (every day). It's for data, sort-kinda like excel. So say I actually wanna load data from an excel sheet... well I can't remember the bloody name of the function (method) for that, pandas.load_xls()?? Let's Google: pandas load excel sheet

(1st hit is docs link). Oh right! It's pandas.read_excel()

Wait but my excel data is on sheet2, and this reads sheet1 by default :(. I can't remember the name of the command (argument) to make it pick a different bloody sheet! Lucky, before I start kicking over tables in frustration, I remember I have the docs page for this method already open, and it's right there... sheet_name

pandas.read_excel(sheet_name="sheet2") 

And down the bottom there's even examples of code how to use it, very handy. This is how the process works.

For more complicated stuff, if it's common google will often lead you to articles, if it's uncommon you'll find yourself brought to comb through stackoverflow. That last one is an amazing resource.

6

u/ItsOkILoveYouMYbb Oct 18 '20 edited Oct 18 '20

Sort of yeah.

It would be a lot easier if you grasp the fundamentals of python and basics of programming, like just dipping your toes a bit in functional programming for the above. For Python fundamentals and that (and some OOP toe dipping too), I'd recommend Python Crash Course 2nd Edition book since I think it does a better job than ATBS, and uses better things like f-strings from the start. At least completing the chapters leading up to the projects at the end.
I also think their example code and hypotheticals make more sense.

Honestly while making your way through that book you'd be able to start the above with Googling. You just need to be able to know how to write basic lines so you can figure out your program. Because everything else you can and will Google.

But then after that you can browse through ATBS for good ideas and seeing more alternatives of script ideas.

I'm pretty sure you could get started on that above idea from reading these chapters in Crash Course:

  • Variables

  • Lists

  • If statements

  • Dictionaries

  • User Input and While Loops

  • Functions

  • Classes (probably not needed for that little script above but still important to get used to)

  • Files and Exceptions

  • Testing your code

In ATBS after, I'd take a look at:

  • Pattern Matching with Regular Expressions

  • Reading, Writing, Organizing Files (likely refresher and alternate perspective on Crash Course chapter)

  • Organizing Files

  • Working with Excel Spreadsheets

  • Working with CSV and JSON files

  • Scheduling Tasks

3

u/hiacbanks Oct 19 '20

this is mini computer science course right here

4

u/selah-uddin Oct 18 '20

i want the answer to this too

2

u/Nanogines99 Oct 19 '20

I did that and went straight into weird types of crazy automation but it will isn't really that hard, you just have to google stuff a certain number of times till it just sticks onto you. In OP's case, installing files every morning the procedure would be: go to the website, finding the new files on the page, downloading it. It isn't really that hard because the scripts are usually almost same for similar automation programs. Just create a set procedure in layman terms and you can easily achieve that if you just Google properly or occasionally involve your programming knowledge

40

u/ItsOkILoveYouMYbb Oct 18 '20

It depends on how deep you want your automation to go.

If you get a csv file once a day that has different data for each day, and you just need to convert that csv to an excel spreadsheet and email it once a day, there's some options of depth here.

1.) You could write a script to automatically pull the data from the csv, write it to excel, connect to email service and send that spreadsheet via email right?

So doing that requires you to start the script manually, and wherever the script is started from needs to have python and any imported libraries installed for it to work. You yourself need to keep an eye out for that csv coming in.

2.) Do all of the above, but do it automatically every time a csv email or file is received. So now you need a script running on a machine 24/7 that doesn't need to be started manually. You could run it on your computer but it'd be safer to run it on a server that is always going.

So you could modify your script to run on a loop where it pings a folder on the network that the csv is going to eventually be saved to, or pings an inbox periodically to check for that email. Once it sees the email or file, it runs your automation code in your script, sends it, then goes back to pinging and waiting for the next csv file to be received.

That server your script is running on would need to have python and those modules installed, and you would need a way of remotely starting it. You'd want your script to let you know if it stops working too. Catch exceptions and email or text or notify you somehow. You could have it automatically restart the script if it can. You could do things on the OS side to automatically run the script at set intervals rather than rely on your script to loop 24/7. You could write a script that automatically runs another script when something happens. The OS can be setup to run that start up / check script once an hour or once a day or whatever you want, as long as the server is up. So then you'd want some way of notifying you if the server goes down in addition to if your script ever freezes or throws an exception.

You could even do all of that on a raspberry pi backed up by a battery if you wanted to.

You can pretty much automate any part of the process, it just depends on how deep you're willing to go as far as I know.

4

u/ryancnap Oct 18 '20

How would you go about having a script running on a VPS you own text message you something to your phone? That's something I'm interested in and don't really know if there's a telephony framework or something you need installed or how you'd go about that

7

u/ItsOkILoveYouMYbb Oct 18 '20

You'd use something like Twilio to interface between the two. Not only do they have an API you can interact with but they also have a python library to help with it too.

https://www.twilio.com/docs/libraries/python

4

u/[deleted] Oct 18 '20

Most phone carriers allow you to send an email like [email protected] and it will send as a text

2

u/bazpaul Oct 18 '20

I recently setup a script to send a push notification to my phone using the tool ‘Pushover’.

It was so easy to setup. Plus you get a tonne of free notifications when you initially set it up

13

u/eloydrummerboy Oct 18 '20

As others have said, "automation" can mean anything that you've written a program to do, where you would have had to do it manually before.

This doesn't specify how that program (or "script") is ran. It's still "automation" if you manually click or type a command to start the program. However, if you can make it so that you don't have to do anything, and the job or task is now done completely by the computer, that would be referred to as "fully automated".

As for the question about how to automate something on another computer, the easiest way is to just "install" and run a script on that computer. At the end of the day, something has to be installed on the remote computer. A computer can't execute code it doesn't have. You might need to control or orchestrat things from a central computer, which can be done several ways, but the code that is going to "do the work" has to be in the computer that needs to "do the work".

12

u/xiongchiamiov xiongchiamiov Oct 18 '20

Let's take a step back and consider reddit. Reddit is, like all programs, automating things that theoretically could be done by hand otherwise. For instance, if we went back to 1995 and you read an interesting thing online and you wanted to comment on it you'd probably email the author, and if they thought your comment was particularly interesting they would add it onto the webpage. Forums are all essentially automating this process, automating the curator's job of having to add each comment onto the page manually. This is so useful that it created an entirely new thing that we could do that previously wasn't realistically possible - you couldn't actually have humans manually add hundreds of thousands of comments a minute and so automating that process created forums as a new thing.

Now, the direct automation of this process might be to write something that checked your email, tried to find the emails that were comments from people and figure out where they should go, put them into the html, and ftp it to your server. What we have instead is a process that achieves the same goal but through a different method that only makes sense in a programmatic world.

And in the real world, that's usually how we approach things. If someone from accounting normally gets one spreadsheet and does some work on it to combine it with another spreadsheet and at the end they have a number that they email to someone else for approval and if it's good they write a check, instead of doing all that with a program we're probably going to like, write an accounting web app that uses some service's api to get some data and store it in sql tables, and on a scheduled time we run some queries to generate something and notify a person to click an approval button and then generate an ACH transfer. Or something. The point is that it's not a direct translation of the previous manual steps.

This is really about software engineering principles. Specifically, what you're doing is requirements gathering to figure out what problem we actually need to solve; people tend to come with preconceptions of a solution so you have to dig for a while to figure out the actual need. And then once you have that, you can design a solution without being constrained by the current manual solution, and then build it.

This is hard at first. It will get easier with practice. And it's an important skill for junior programmers to build.

4

u/idwpan Oct 18 '20

Like, how do you actually automate stuff like excel, word, and other applications

There are libraries available to do things like convert from another format (like .csv to .xlsx), or read/write directly from the desired file type.

Do you always have to go to the terminal to run an automation program or do you have to create an application on the desktop

They can be run manually, or as a service. Linux systems have cron or systemd that can be used to launch services in the background at certain times. I have a Reddit bot for instance, that I run with systemd on a Raspberry Pi, that starts automatically at boot, and restarts if the script crashes for whatever reason. That way I can just plug the Pi in and it will start the bot automatically, without me needing to login and do anything manually each time.

are there certain things I have to download

You can search for libraries to do what you are looking for, for instance openpyxl can be used to read/write .xlsx files. It is recommended to use some sort of virtualenv environment, which separates your project environment from your global system environment. This keeps things nice and clean for each project you are working on, that may require different libraries. Libraries can usually be installed with pip, just by running pip install openpyxl or whatever library you are trying to install.

is it possible to automate someone else’s computer from your computer

If you have remote access (RDP, SSH, VNC, etc) and you can run anything like you were sitting at their keyboard, Python stuff included.

2

u/iiMoe Oct 18 '20

Look into com objects and pywin32 if u want real desktop apps automation

2

u/[deleted] Oct 18 '20

First, if you do manual work with excel, word, etc; there are plenty py libs to "automate" your work. This might be simple, but it is automation. I've done simple automation work like this in the past: grab data, post/process, present/save numbers/charts into docs/slides/sheets/pdf. A lot of time saved, e.g.: scripts generating slides with graphs for executives only takes seconds instead of hours (proofing time not included).

Last, once your py scripts do what they want you to do, like my example above, you could then work on wrapping them up. Either make a nicer presentation layer, e.g.: GUI, web dashboard, etc., or making them run in the background periodically.

The SW people will be able to answer your questions in the proper SW terms and processes. I'm just a hack that try to work less hours for the same pay. :)

2

u/emat66 Oct 18 '20

Just throwing another example out there since I have nothing to add beyond others.

I had 200 postal codes and I needed to find out what city they corresponded to... to do this I needed to go to the Canada post website, type in the code, record location

I would have been happier smashing my face against broken glass and rusty nails

So storing my postal codes in an excel file, I used pandas to read in the codes, selenium to take over my keyboard and go to the website type in the postal code, I then had the program create an excel file of the postal code and location

Automating word documents I use the docx library .... I generally do it when I have something like monthly reports or any data that will change while I complete the report ... it’s pretty awesome when my colleagues create reports that are updated as of 3 weeks ago and mine are updated as of 3 minutes

2

u/sj-development Oct 18 '20

Windows and Linux have built in ways to schedule executing a program, is: a python script

You can also write a script so you can run one command and it'll do 100 things

2

u/Crypt0Nihilist Oct 19 '20

Remember that at the heart of things, what is happening is that a set of instructions is being run to tell your computer to do x, y, z.

You might tell your computer to run that set of instructions every hour, or day or however long if you want. You also might feed it some information about the world by passing it info as arguments when you run it from the command line, or get it to find out things itself as part of the list of instructions so it can do different things based on what's happening. Or, you might create a friendly user interface to pass bits of information to it so it can choose how to respond. Finally, you might give it a friendly icon you can click on.

There you go, that covers most programs and automation that I can think of!

There's nothing special about a python application, they're just like the ones which run on your computer normally - some of them will literally be python apps!

2

u/JayGridley Oct 19 '20

Let me give you an example of a real world issue I solved using Python and several things out of that "Automate the Boring Stuff with Python" book.

Problem: 30 person team and each person has to log into a website on a regular basis and submit a ticket for a client for each product we've "completed." Many of these are 3 to 4 products each and often 10+ clients each time. So you are talking about 30+ tickets easily. It takes a lot of time.

Solution: First thing was to create a report out of SalesForce using the built in reporting function to give us a complete report of all clients in that phase and to pull all relevant client information needed for the ticket.

Now this is where the Python comes in. I created a script that does the following:

  1. Launches the ticketing system.

  2. Logs into the ticketing system with credentials provided in the script.

  3. Populates all of the ticket fields with data from the Excel report we got out of SalesForce.

  4. Submits the ticket.

  5. Rinse and repeat for every entry on the report.

So now those 30 people no longer have to do this process saving hours each week.

I used openpyxl to deal with the Excel file and Selenium to deal with the browser aspect.

2

u/[deleted] Oct 19 '20

Your automations are just programs that do things people used to do, and programs can live pretty much anywhere. Since programs can live anywhere you need to figure out where to put the code. That largely depends on the following:

  1. Where does the input come from? Is it an excel spreadsheet? An email? An image? Is it stored on my co-workers desktop or in my phone?

  2. Where does the output go to? Which person needs the results of the program? Do I email it to Jan in accounting? Does it go in a folder on our shared file server? Do I open it up and look at it? Do I upload it to a website or application?

  3. What kind of resources can I spend on this thing? Do I have a lot of time and money to throw at this or do I really need a cheap solution? Will this be running 24/7 and tie up the ram/cpu cycles on my personal computer?

  4. How reliable does this need to be? If it breaks will the company come crashing down or will Bob need to just do the task manually for a couple hours?

  5. How secure does this need to be? Am I dealing with generic unidentifiable data, the kind of stuff that would be useless to a potential hacker? Am I working with social security numbers and super sensitive information?

If you have a program that needs to run based on events rather than a schedule, requires high availability and is business critical you can justify running it on a cloud platform instead of your pc. Most beginner automations are just fine running on a set schedule but if you really need the automation running in real time, like in response to events, you'll need something more sophisticated. Take a look at aws lambdas samples, many of them are the kind of things you'll find in Automate the Boring Stuff, but set up on a low cost high availability server.

2

u/Seawolf159 Oct 19 '20

Sorry, but this is completely unrelated to the OP, the answers already given should be enough to help him out. How is it that a post like this gets a million reactions, but every time I ask a question here, I get only 1 response. How does it work?

4

u/Fledgeling Oct 19 '20

Because more people feel qualified to answer a broad/general question like this than something very specific and more advanced.

2

u/integralWorker Oct 18 '20

Do whatever you'd do in Excel in Pandas+Numpy+Matplotlib and then you will understand.

1

u/SGS-Tech-World Oct 20 '20

One automation we did is ..

Problem Statement -

We receive some invoices in PDF format which has data like invoice number, PO number, item, quantity , prices etc. needs to be uploaded on to portal.

Solution

  1. Convert PDF to text format. ( using PyPDF2 or something similar)

  2. Find out the patterns and extract data ( use mostly string functions and re where needed )

  3. Write to excel using xlsxWriter.

This was part of one RPA process , the RPA then picks up the file and pushes the data into portal, we can use Selenium also if RPA was not there.

BTW On Windows .. you rename your python script to .pyw so windows scheduler can run it directly..

0

u/[deleted] Oct 18 '20

[deleted]

5

u/AceBuddy Oct 18 '20

Also important to note (for anyone that doesn’t use logging regularly) is that if you have any print statements you’re not going to see them in cron, so you should use the logging library and make a log file to easily monitor the programs behavior.

1

u/R-2-D-2x Oct 18 '20

Epic

0

u/[deleted] Oct 18 '20

[deleted]

1

u/R-2-D-2x Oct 20 '20

The things made possible with Python!

2

u/[deleted] Oct 18 '20

[removed] — view removed comment

1

u/emat66 Oct 18 '20

I assume your data was stored in an excel file?

1

u/Kontrolgaming Oct 18 '20

Could you do this for say

Job Applications, because every job wants you to log into their site and then post your job details, which feels like a job finding a job. Thanks!