Back Read And Write Excel Files In Python Using Openpyxl In PyCharm- Excel Styling and formatting Python

40-50
Created By: Mukesh otwani
Published At: 2020-10-27
Search Query: Python Read Files,Python Write Create Files
Tags: read excel in python write excel in python write excel in python openpyxl read write excel in python excel read and write in python excel formatting in python conditional formatting in excel using python python tutorials python for beginners
In this video, we will discuss different ways to read excel in python and we will also discuss how to perform write operations as well.

We have many libraries which allow us to read excel but I personally prefer openpyxl. You can also go ahead with panda library.

Python Excel Lib
https://www.python-excel.org/

Python Series
https://www.youtube.com/playlist?list=PL6flErFppaj0v1XGN-og_hL4Rw2v2lidK

Blog
http://learn-automation.com/

All Playlist
https://www.youtube.com/c/Mukeshotwani/playlists

Connect with us:
Linkedin- https://www.linkedin.com/in/mukesh-otwani-93631b99/
Instagram- https://www.instagram.com/mukeshotwani/
Facebook Group- https://www.facebook.com/groups/256655817858291
Facebook Page- https://www.facebook.com/SeleniumWebdriverMukesh
Twitter- https://twitter.com/MukeshOtwani
Blog- http://learn-automation.com
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@ibatuhanozdemirr
2021-04-10

For some reason your video appears as 4th video when i search openpyxl. But this is miles better than the first 3. Great job.

@Mukeshotwani

Thank you 😊Batuhan

VidTutorAssistant (GPT-4)

Mukesh Otwani: Thank you so much for your kind words, @ibatuhanozdemirr! It's my mission to create very informative and easy-to-understand Python tutorials for our subscribers, and I'm thrilled to know this video on openpyxl met those standards for you. Your support helps us reach more people who want to learn Python. Remember to share these tutorials with your fellow Python enthusiasts so that they, too, can benefit from them. Once again, thank you for your support and look forward to more Python programming content in upcoming videos. Have a great day!


@gangadharaswamy4147
2022-04-03

for me getting error like wb=workbook()


TypeError: 'module' object is not callable

@Mukeshotwani

Hi Swamy please install openpyxl in Pyrcharm to fix this issue.

VidTutorAssistant (GPT-4)

Hi @gangadharaswamy4147, thanks for reaching out. In Python, this error typically means that you're trying to call a module instead of a class or function. In our context, the issue seems to be with the instantiation of the Workbook class from the openpyxl module.

Make sure you've correctly imported the Workbook class before you're trying to create the 'wb' object. Your import should look like this:

```python
from openpyxl import Workbook
```
And then, you can create an instance of the Workbook class like:

```python
wb = Workbook()
```
I hope this helps! Feel free to post if you have further doubts. Looking forward to helping you learn this awesome programming stuff. Happy coding!


@myWayIn619
2020-12-07

does it overwrite previous excel sheet?

@Mukeshotwani

yes but without loosing data. If you give new path then it create new copy with existing data too.

VidTutorAssistant (GPT-4)

Thank you for your question, @myWayIn619! When we save our changes, if we provide the same file name as the original excel sheet, it will overwrite the existing excel sheet. However, if you provide a different name, it will create a new excel sheet retaining the old data and additional new data. You can see this process explained in the segment of the video where we create a new file "report.xlsx." I hope this helps! If you have any more questions, feel free to ask.


@Shortzzz_123
2022-05-18

How can someone be soo perfect in explaining each and every step 😍

@Mukeshotwani

Thanks Shabana for such a nice comment. This made my day. Happy Weekend ☺

VidTutorAssistant (GPT-4)

Thank you for your kind words, @Shortzzz_123! I always strive to present each step as clearly as possible so everyone can understand easily. It's awesome to know you find the videos helpful. If you have any questions or there are specific topics you want me to cover in future videos, feel free to let me know! Happy learning 😊.


@manzarzia2536
2021-04-21

How to add another sheet Sir?

@Mukeshotwani

Hi Manzar, please use create_sheet('sid1') method.

VidTutorAssistant (GPT-4)

Mukesh Otwani: Hi @manzarzia2536, thank you for your question. If you want to add another sheet, you have to use the 'create_sheet()' method from the 'Workbook' class in openpyxl. Here's how you do it:

```python
from openpyxl import Workbook

wb = Workbook()

ws1 = wb.create_sheet("MySheet") # creates a new sheet
```
In this code, 'ws1' now refers to the new sheet you've just created. If you want the new sheet to be at the beginning of the workbook, you can add an index parameter like so: `ws2 = wb.create_sheet("MySheet", 0)`. This adds the new sheet at the start of the workbook. You can work with this new sheet like any other, reading and writing data to it. Remember to save your changes with `wb.save('path_to_my_workbook.xlsx')`.

I hope this helps, don't hesitate to ask if you have any other questions. Happy coding!


@NavjotSinghpatiala
2021-05-26

when i do this: import openpyxl

i am getting this Process finished with exit code 0.
nothing else, colour of the import is also dull or gray

@Mukeshotwani

Hi Navjot, mport openpyxl but until you dont use it will show in gray. Start using openpyxl module so it will change.

VidTutorAssistant (GPT-4)

Mukesh Otwani: Hi @NavjotSinghpatiala, thank you for reaching out with your question! Seeing 'Process finished with exit code 0' is completely fine and indicates that your Python script has finished without any error, which is a good sign. Regarding the dull or grey color of 'import openpyxl', it simply means that you have imported the openpyxl module but have not used it in your code yet. Once you start using functions or classes from the openpyxl module, this grey color will change. It's a feature of IDEs like PyCharm to show unused imports in grey. Feel free to explore openpyxl more and apply the concepts we discussed in the video. I hope this helps! Let me know if you have any more questions.


@sandeepnallala48
2021-09-03

I came across this channel for 1st time and its really cool to learn these things from such a teacher, thank you sir : )
it helped me. thanks sir : )

@Mukeshotwani

Thank you so much Sandeep for such a beautiful comment.

VidTutorAssistant (GPT-4)

Mukesh Otwani: Thank you so much for your kind words, @sandeepnallala48! I'm really glad to hear that you found the video helpful. It's always great to welcome new learners to the channel. Stay tuned for more on Python programming. Don't hesitate to ask if you have any questions or topics you'd like covered in future videos. Happy Coding!


@yomismo5711
2022-03-14

Very Good explanation. Clean, step by step and clear.
I recommend this video a lot.
Thank you very much! πŸ‘

@Mukeshotwani

Glad it helped! Thank you so much.

VidTutorAssistant (GPT-4)

Mukesh Otwani: Thanks a lot @yomismo5711! I'm glad you found the explanation clear and helpful. Sharing knowledge is my aim, and seeing comments like this makes it all worthwhile for me. I appreciate your support and recommendation. Stay tuned for more informative content! Cheers! πŸ‘πŸ‘


@nisarpasha615
2022-02-28

Can you please upload reading the excel having formula to a particular cell

@Mukeshotwani

HI Nisar, please use below
data_only=True
Example
wb = openpyxl.load_workbook(filename, data_only=True)

VidTutorAssistant (GPT-4)

Hi @nisarpasha615, thank you for your suggestion! That's a great topic and I agree, it would be quite useful to know how to work with Excel cells that have a formula. Rest assured, it is in my pipeline for upcoming content. In the meantime, if you're dealing with such a scenario, you might want to explore using the 'openpyxl' or 'xlrd' libraries. These allow you to read the formulas in cells by using the '.value' attribute. Stay tuned for the tutorial and don't forget to subscribe if you haven't already! Happy coding.


@muporepain
2021-08-08

Excellent videos, but without subtitles I don't understand a thing from this accent... Thank you for the video.

@Mukeshotwani

Sorry about that Afrodite , I will try to add subtitles from next videos.

VidTutorAssistant (GPT-4)

Mukesh Otwani: Hi @muporepain, Thank you for your valuable feedback. I sincerely apologize for the inconvenience caused due to the accent. I'll ensure to add subtitles in the upcoming videos for a better understanding of the content. I truly value your engagement with my channel. Stay tuned for more Python programming videos.