Automate Excel Spreadsheet with Python
HTML-код
- Опубликовано: 8 фев 2025
- In this Python tutorial, I will be covering how to automate Excel spreadsheets using win32com (pywin32) library in Python.
Things I will be covering in this tutorial:
1. Create a new Excel spreadsheet
2. Write/Read data from an Excel spreadsheet
3. Save the Excel file
Buy Me a Coffee? Your support is much appreciated!
----------------------------------------------------------------------------------------------------------------
PayPal Me: www.paypal.me/...
Venmo: @Jie-Jenn
To install win32com library: pip install pywin32
why win32com?
As a Windows user myself, the win32com library provides me with the flexibility to communicate multiple applications and integrate them with ease. Especially, I can control the applications while the applications are open. The other advantage win32com has over other Excel Python libraries is that, using win32com to link to an COM object application, I will have access to the entire object models vs other Excel Python libraries will only have a limited features available.
win32com (pywin32)
A Python extension for Microsoft Windows provides access to the Win32 API. Using Win32 API, we will be able to create and use COM objects to automate applications such as Microsoft Excel, Outlook, Word, Access, PowerPoint, or event Photoshop. On top that, we will be able to automate a wide range of features on Windows.
Support my channel so I can continue making free contents
---------------------------------------------------------------------------------------------------------------
Patreon: / jiejenn
By shopping on Amazon → amzn.to/2JkGeMD
More tutorial videos on my website → LearnDataAnaly...
Business Inquiring: RUclips@LearnDataAnalysis.org
Tags:
#Excel #Python #win32com
thanks Jie, this simple to understand that it is possible modify data in excel with Pyton , no need VBA
Thank you for preparing the awesome video. This video is very easy to understand. I have the motivation to continue learning python.
Thank you thank you. Now I know how to use VBA Object modele in my Python code. I search this many years. I didnt think just to take from Microsoft doc. So simple.
Thank you. It has been of great help.
Glad the video helped.
Awesome video, keep up the amazing work! :)
thank you
Nice,
What is the difference with xlWings? which is better? Tranks!!!
xlwings is just a wrapper around pywin32 which offers a simpler usage to work some of the Excel models.
@@jiejenn Thanks bro :)
Hi Jie, Can you tell if pywin32 can help with the below use case?
1.I want to import Excel and read as Dataframe
2.Then do some Spacy based text analysis. I will also use other packages.
3.Finally store the results (scores) in New columns
4.Export the Excel
Pretty sure it is possible. Essentially you are just referencing Excel application in Python, it doesn't interpret other things you do.
You should add chapters to your video. Makes it more useful. Thanks
Thanks
Glad the video helped.
Thanks for this video! Could you advise which would be the best way to copy data between different books? My main goal would be to create a database by getting data from various excel books in just one. Thanks in advance!
If you are just importing data from Excel files to a database, I would go with pandas.
can i also put formula and return the value instead of none?
Can you be more specific?
At work I have all these different excel spreadsheets I need scan through for information. I want to create a prompt that ask for the name of the user and scans through these excel spreadsheets n then shows me predetermined useful information I would need.. is this possible with python?
Yes
Great video! How do I dynamically copy the range from A1 to E5 without specifying E5? like in VBA you could start from A1 and do xldown, xlright...
In Python, you would fetch the last row number as LastRow = wsEntry.Cells(wsEntry.Rows.Count, "A").End(-4162).row where -4162 represents xlUp numerate value.
docs.microsoft.com/en-us/office/vba/api/excel.xldirection
Will this work with Excel 2007, do you know? Thanks.
Because you are directly accessing Excel application, the method will work with Excel 2003 and later.
Could this work with LibreOffice Calc as well? Thank you for your reply...
I don't use LibreOffice, but looks like you can run Python script directly from the app.
help.libreoffice.org/6.3/en-US/text/sbasic/python/main0000.html
@@jiejenn Merci beaucoup!
How to bring it to foreground instead just opening in the background... Please Help....
wb.Activate
@@jiejenn Actually I'm using a Word Document so when I wrote word.Activate its giving error
@@jiejenn Here is my code...
word = win32.Dispatch('Word.Application')
print(type(word))
word.Visible = 1
print("opened")
time.sleep(10)
word.Quit()
print("quitted")
Can you help how to make it to foreground
You didn't even create the word document object. Word Application is not a Word file.
@@jiejenn yes sir it's enough if my word application opens in foreground but its not instead opening in background
How can I open another worksheet.
use workbook.add to add a new worksheet.
Could you pls tell how do you add custom calculation on pivot table via win32com? i mean the usage of win32com.client.constants.xlPercentOfParentRow?
Your question is quite specific, it is difficult to explain in a few words. I would recommend you post your question on Stack Overflow for answers.