Automate Excel With Python - Python Excel Tutorial (OpenPyXL)

The first 1,000 people to click this link will get a free career coaching session courtesy of Career Karma:

Welcome to another video! In this video, I will cover how we can use python to automate Excel. I’ll be going over everything from creating workbooks to accessing individual cells and stylizing cells. There is a ton of things that you can do with Excel but I’ll just be covering the core/base things in OpenPyXl.

📄 Resources 📄
OpenPyXL Docs:
Code Written in This Tutorial:
Fix Pip (Windows):
Fix Pip (Mac/Linux):

⭐️ Timestamps ⭐️
00:00 | Introduction
02:14 | Installing openpyxl
03:19 | Testing Installation
04:25 | Loading an Existing Workbook
06:46 | Accessing Worksheets
07:37 | Accessing Cell Values
08:58 | Saving Workbooks
09:52 | Creating, Listing and Changing Sheets
11:50 | Creating a New Workbook
12:39 | Adding/Appending Rows
14:26 | Accessing Multiple Cells
20:46 | Merging Cells
22:27 | Inserting and Deleting Rows
23:35 | Inserting and Deleting Columns
24:48 | Copying and Moving Cells
26:06 | Practical Example, Formulas & Cell Styling

💰 Courses & Merch 💰
💻 The Fundamentals of Programming w/ Python:
👕 Merchandise:

🔗 Social Medias 🔗
📸 Instagram:
📱 Twitter:
⭐ Discord:
📝 LinkedIn:
🌎 Website:
📂 GitHub:
🔊 Podcast:

🎬 My YouTube Gear 🎬
🎥 Main Camera (EOS Canon 90D):
🎥 Secondary Camera (Panasonic Lumix G7):
📹 Main Lens (EFS 24mm f/2.8):
🕹 Tripod:
🎤 Main Microphone (Rode NT1):
🎤 Secondary Microphone (Synco Wireless Lapel System):
🎤 Third Microphone (Rode NTG4+):
☀️ Lights:
⌨ Keyboard (Daskeyboard 4Q):
🖱 Mouse (Logitech MX Master):
📸 Webcam (Logitech 1080p Pro):
📢 Speaker (Beats Pill):
🎧 Headphones (Bose Quiet Comfort 35):
🌞 Lamp (BenQ E-reading Lamp):
🌞 Secondary Lamp (BenQ Screenbar Plus):
💻 Monitor (BenQ EX2780Q):
💻 Monitor (LG Ultrawide 34WN750):
🎙 Mic Boom Arm (Rode PSA 1):
🎚 Audio Interface (Focusrite Scarlet 4i4):

💸 Donations 💸
💵 One-Time Donations:
💰 Patreon:

⭐️ Tags ⭐️
– Tech With Tim
– Python
– Excel
– Python Tutorial
– Python and Excel
– Automating Excel

⭐️ Hashtags ⭐️
#TechWithTim #PythonExcelTutorial


48 thoughts on “Automate Excel With Python – Python Excel Tutorial (OpenPyXL)”
  1. Hi Tim , I am new to learning python could you please tell me from which playlist I have to start to watch video and how ahead to step by step .

  2. Actually, this sort of scripting could be used to insert data and images and stuff.. from elsewhere, files, or folders of files and concatanating them into your .xlsx for presentation format.

  3. Recently started learning python! Fantastic tutorial, keep up this great content! Never thought Python could automate so much. As a financial analyst I use excel every day. Now with this knowledge, I can automate tasks that are somewhat repetitive and save loads of time!. 

    Just had a problem with the last line of code, can't seem to get it to work. When entering the fonts code, it shows up as "TypeError: 'module' object is not callable".

    Any ideas? here's my code: 

    for col in range(1,6):
    ws[get_column_letter(col) + '1'].fonts = fonts(bold = True, color = "0099CCFF")

  4. How does it know to iterate through rows? I know you say “for row in range” but the name row is arbitrary right? You coulda said “for i in range”

  5. When you save a workbook, is there a way for the calculations in the workbook to be executed so that the next time you run it you get the newly calculated values based on any new inputs without actually opening the excel file. I have tried using the formulas library for this. But I am running into problems with complicated calculations. I was wondering if there was another way to do this. Thanks!

  6. Is it possible to write a code that prints selected worksheets in a particular order.? Like using the while loop?

  7. Hi is it possible to install this module when using MS Visual Code? and how to? all CMD commands mentioned do not work, thanks

  8. Hi Tim, sorry if this is kinda silly question. it is possible if openpyxl to read multiple file in a folder. Process a file, close process and process next file?

  9. Awesome tutorial and my only frustration is time limitations to apply all that you have shared. Thanks, and God bless

  10. Who the heck is this guy!! Just amazing trainings and super easy to follow along. Keep them coming Tim, much appreciated.

  11. Hello teacher, may i ask a question , im looking for something like VBA with python, can we do that? writing macros for excel with python?

  12. @Tech With Tim, for the first for loop, for row in range(1, 11) how does the compiler know youre trying to loop through the worksheet?

  13. Excellent.But I want to know if there is any way to insert a cell instead insert a row ?. Can you help me. Thank you

  14. YO need help after I install like you said but when I run the import part it says there was no openpyxl so my head crack to see any trouble

  15. New to python and starting my own project working with excel files. Tried to go through the documentation for openpyxl and was wasting alot of time learning unnecessary information based on what I was trying to accomplish. You've done a wonderful job with this and I was able to easily follow along and experiment along the way. Put together a py file for my notes on this module. Tim is Great!

  16. how to append one (new)data frame to the excel sheet with OpenPyXL.Please let me know

  17. recently got into learning python.
    my mind is blown with the versatility of python. I now wish i had done software engineering back in school..

  18. As Tim noted, you cannot save the spreadsheet changes if it is opened in Excel (or in my case, Libre Office) for 'normal' use.
    I did figure a sort of workaround to this problem.
    If you open the spreadsheet as read only, your python code will run without a problem. Now to view the changes simply reload the spreadsheet.
    The reload command in Libre Office can be found under File >>> Reload (or press the keys Alt, f, l in that order(Note l is lowercase L))
    Yes, you have to remember to reload the spreadsheet every time you make a change, but it is quicker than the close, reopen way, you could put a reminder in your python code.

    Note, if you want to edit the read only open file, there is a button on the top right that says Edit Document. but remember after editing it needs to be saved and reopened as read only.
    Use the python command try when doing a workbook save, and if it throws and exception, then ask the user to go through the close/reopen read only routine.

  19. i tried this in google colaboratory and i couldnt make it work! 🙁 could someone help me?
    im trying to delete all the . (dots) from a specific excel row

  20. Hi
    you can make a video how to takes videos from instagram and upload them to tiktok automaticly. Like 100 videos per day. It is possible?

  21. Hello, plz I need help!
    I got a table that got a 3 lines and 4 columns (like what's shown)

    C D R

    A 0 0 F – F

    B 0 0 F – F

    I want to automatically create other tables which will rewrite , the "0" into "1" , one by one , writing every table/possbility

    also in the "R" row , for each "0" there is an "F" and if there is a "1" we write a "T" ; following the order from left to right ; so the lines can have :

    | 0 | 0 |F – F |

    | 1 | 0 |T – F |

    | 0 | 1 |F – T |

    | 1 | 1 |T – T |

    I am completely a newbie in excel and I need this for a homework , plz help

    I think the "R" row needs variable values , do they even exist in excel ??

    Pretty Please take a piece of your time to explain it to me , thx!

    BTW, the "T" and the "F" are not logic functions , thx !!

    PS: I got excel 2019

  22. Avatar of CHITUS💖⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻ CHITUS💖⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻ says:

    Actually did something similar a month ago, where I converted the excel file in to base64 in the end and sent it through an API to then generate it back into xlsc format through typescript.
    PyXl is amazing btw, anyone who watches this should try it. Very easy to use.

  23. I think using sheet.cell(row = r, column = c) is also good, rather than converting the column number to a character, also I think the reason that Grades.xlsx didn't show any data is because you used, but at the time, the active sheet was an empty one.

  24. I've been learning Python by following a book and got confused and stuck for so long until I found this video. Thanks

  25. Thanks for your tutorial, I ran into a problem when I was automating my excel file, I want to fill another column ( date column ) according to the previous column so that it is filled with a condition. How can I do that? Thanks in advance.

  26. this data is json you can simple use pandas and do it in 2 lines of code: df = pd.read_json("data.json") , df.to_excal("NewGrades.xlsx")

Leave a Reply

Your email address will not be published.

Captcha loading...