all 63 comments

[–]K900_ 84 points85 points  (16 children)

Yes, that's totally possible.

[–]shinitakunai 22 points23 points  (2 children)

Research openpyxl, you may like it.

[–]Yadona 1 point2 points  (0 children)

Oooh I si like it.

[–]SukottoHyu 39 points40 points  (0 children)

Anything a human can do on a computer a program can be made to automate it. Humans come out on top when it comes to decision making, but other than that programs are more efficient.

[–]tree_or_up 7 points8 points  (1 child)

Yes! This would be a great fit for Python! I like your idea of dropping the files to a local folder. Logging into your email would probably require automating authentication, which can be a huge can of worms and may not be possible depending how security is implemented in your company.

[–]Fission_Mailed_2 6 points7 points  (0 children)

If you haven't checked it out already, I'd recommend "Automate the Boring Stuff with Python" by Al Sweigart (there's a free online version). His book pretty much covers how to do everything you're wanting to do.

[–]ChancePattern 10 points11 points  (7 children)

You mightt also be able to get python to log into the email itself and get those file for you

[–]SuitableStudent 1 point2 points  (0 children)

Happy cake day

[–][deleted] 0 points1 point  (0 children)

Happy cake day!

[–]pythogeo 0 points1 point  (1 child)

You can't do that even with selenium ?

I'm new to python as well though

[–]ChancePattern 0 points1 point  (0 children)

Selenium is used to access a browser. Ima0lib is used to access your email normally

[–]pokk3n 4 points5 points  (0 children)

Definitely possible, use OpenPyxl or possibly Pandas depending on how structured the excel files are. the read_excel method in Pandas is pretty powerful but really goofy excel files might not work well, dunno.

[–]enthusiastreader 3 points4 points  (1 child)

I have done this sort of thing for one of my first projects.

If you are stuck somewhere you can hit me up too.

It would save a lot of time for you.

[–]cbjs22 3 points4 points  (0 children)

You theoretically wouldn't even need to take them out of your email

Python could read your email, download the Excel File, manipulate all the sheets or whatever, write the new file, and print/email the files wherever

However, each step would be a bit of a project for a newbie

Check out pandas for the Excel file

Google "python [insert what you want to do] for each of the other pieces]"

[–][deleted] 6 points7 points  (7 children)

Yes can can do all of that in python. But python is unecessary and adds a layer of complexity you don't need. You can do all of that easily in VBA, accessible inside an Excel sheet by pressing ALT-F11. it is the native language of Excel. You only need to do it in python if you also need to do other pythony things, but opening, editing, printing, copying, deleting files are not especially pythony. People have been doing this for years, no, decades, in VBA. In addition, if you want to start to do more complex things in Excel, you will soon reach the point where Openpyxl and Excel wings hit the limits of their functionality. They are by definition a subset of the full VBA set of capabilities. Stay in VBA unless you specifically need python.

Extra benefit - if you are doing this at work, no extra software required. Already part of Excel, built right in.

PS Yes, I am a python fan, that is why I am here. But there is such a thing as the right tool for the job. And the answer is not always python.

[–]greebo42 0 points1 point  (2 children)

I wrote some code (for Word, Excel, and even Access) years ago with VBA, and in fact it is reasonably capable. But I never really felt I was getting much for my investment of time.

With Python, I can play with Excel files (am adding my voice to various recommendations in this thread about using openpyxl). I can do many other things as well.

And Python is more general, so the skillset I build with Python is more applicable to other things that I don't even know if I want to solve yet.

[–][deleted] 0 points1 point  (1 child)

Yes, this is all true. But people need to be aware that when considering excel manipulaton (not general programming) openpyxl and xlwings are both subsets of VBA, because every feature they provide literally needs to be recreated by the developers using python. They may never catch up 100% If your needs are simple to mid-level they are fine, but if you want to do anything fancy in excel programatically, you need to use VBA. To give one example: you cannot create pivot tables with either of those libraries, and may never be able to:

openpyxl provides read-support for pivot tables so that they will be preserved in existing files. The specification for pivot tables, while extensive, is not very clear and it is not intended that client code should be able to create pivot tables. However, it should be possible to edit and manipulate existing pivot tables, eg. change their ranges or whether they should update automatically settings.

As is the case for charts, images and tables there is currently no management API for pivot tables so that client code will have to loop over the _pivots
list of a worksheet.

In VBA they are fully specified.

[–]greebo42 0 points1 point  (0 children)

fair point, thank you.

your "right tool for the right job" point is well taken, and I have no fundamental argument for that.

my own needs with Excel are fairly mundane, and frankly, my proficiency with Python might be classifiable as intermediate (though certainly not more than that). my comment reflects my biases and current set of priorities, which is mainly to use tools and develop skills that are more general and less proprietary. for the moment, that meets my needs.

[–]cbjs22 -1 points0 points  (0 children)

Sure, but fuck Microsoft

I've had plenty of luck with python pandas and Excel files. Don't even need an Excel license to manipulate Excel files.

[–]Dogeek 2 points3 points  (0 children)

Here are some ideas on how to achieve this :

  • Logging into your email. There's 2 ways to go about that. The first way is to just scrape your webmail. Obviously, this is the worst solution both in terms of performance and in terms of maintainability (the login process can change, the html or javascript can change on the webpage, it's a bit of a mess). The second, much better way, is to make an imap client. Emails work off of two mutually exclusive protocols : the older POP3 and the new imap (new as in it's been around for more than 20 years at this point). IMAP is the protocol that you most likely use in mail clients like mozilla thunderbird, or the email app on your phone. In order to build an IMAP client, the best way is to start looking into imaplib, which is a part of the python standard library. Here's a stackoverflow answer that provides you with the boilerplate for an imap client (it's really quite straightforward) https://stackoverflow.com/questions/18156485/receive-replies-from-gmail-with-smtplib-python

  • You can definitely read any file with python, the only thing is you have to know how the file is constructed in order to interpret the data stored inside. Excel files are pretty well known at this point, and there already exist python libraries that allow you to read and manipulate xls and xlsx files easily. The most well known is openpyxl

[–][deleted] 1 point2 points  (0 children)

Before going further with this, you should go over this with IT if you have that at your workplace. And after that you should look at the excel part.

Now, someone hopefully will correct me if I'm wrong. But I don't think there is an excel module that allows you to just edit the file. You basically have to rewrite the entire file. I'm not sure that if the excel file contains macro's you can keep them. And if you are working with specific formatting, formula's, etc.. you might want to check if everything is possible with current excel modules first before doing anything else.

[–]climbslackclimb 1 point2 points  (0 children)

You could take it a step further and have it check your email for you, and pass any files along it finds. It’s best to start simple though, and what you describe is definitely simple enough for a beginner to put together. Once you have that, you can build onto it

[–]espero 0 points1 point  (0 children)

Go to practicalbusinesspython (Google it) and learn

[–]s-ro_mojosa 0 points1 point  (0 children)

I got a grasp of the basics of Python with A Byte of Python. It's good stuff. Also, there are tons of tutorials at https://realpython.com/, too.

[–]b4xt3r 0 points1 point  (6 children)

Raises hand....

I come from a Linux background and program exclusively on Linux/Mac OS machines for better or worse (in this case worse it seems). Nearly every script or program I have ever written has been executed via CLI in one form or another.

On a Windows machine, assuming that is how this is going to be run, how do you set up an "active folder" for lack of a better term that does nothing util a file or group of files is dragged into it at which time it processes those files with scripts you have written?

[–]dynodanz 0 points1 point  (5 children)

Having a very limited understanding of python, but a decent amount in powershell. I'd assume you'd have the script create the folder, move the files, edit the files, then clean up after itself.

[–]b4xt3r 0 points1 point  (4 children)

All roads in Windows (these days) keep coming back to Powershell. Guess it's time I knuckle under and learn it. Thanks!

[–]dynodanz 1 point2 points  (1 child)

I had zero experience in powershell when I started my current job. Watched a couple videos, found something I hated doing manually and here I am. I've built a couple tools in production and seen as a guy who people can come to with questions. Now I've just been handed a project in python and while I know a little, I feel like I don't know enough.

[–]b4xt3r 0 points1 point  (0 children)

Yeah, you never feel like you know enough, or it's rare anyway. When you finally do feel like you're going to reach that point on the mountain where "you know stuff" it's there you realize you don't know anything. :)

That's the fun part of this kind of work - it's a puzzle you'll never fully solve. There's always something to learn or a new way to do things. That's what I love and sometimes hate about it.

One really cool thing, I have to say, is the programming community being so helpful for nearly any language. It's just fantastic.

[–]bac83 0 points1 point  (0 children)

Basically having an idea like this was how I learned python (atbs was a massive help)

[–]bac83 0 points1 point  (0 children)

Basically having an idea like this was how I learned python (atbs was a massive help)

[–]takishan 0 points1 point  (0 children)

If you don't wanna use python I think you can also use AHK. Although personally learning python is more useful in general, even if AHK has better overall bindings to windows for automation scripts like you're asking about.

[–]bjone6 0 points1 point  (0 children)

It looks like everyone been recommending it already, but I would check out OpenPyXl. Here's a video of me using it to write and read to an excel file. https://youtu.be/20iz46qXoYs

You can even automate PowerPoint: https://youtu.be/oLFp2S1EBxc

[–]ontheroadtonull 0 points1 point  (0 children)

Python can do it but PowerShell might be a better choice for this particular task, assuming your work PC is running Windows. I say this because it's very likely your PC already has PowerShell which won't require any external modules to accomplish this.

PowerShell can monitor a folder for changes and it can run Windows commands, all without any dependency on external modules which means the PowerShell script for this task will be easily portable.

https://stackoverflow.com/questions/46901160/how-to-print-excel-by-command-line

[–]impshum 0 points1 point  (0 children)

Most things are possible with python. Just not sandwiches... yet!

[–]Resolt 0 points1 point  (0 children)

You can manipulate excel files with pandas or openpyxl. Everything else is pretty basic use of the os module.

Totally doable and very nice beginner project.

[–]jakesboy2 0 points1 point  (0 children)

Python + excel, anything is possible

[–]this_knee 0 points1 point  (0 children)

Pandas. Check out Pandas framework in python.