Create Excel file in Python


In this Post we will see that how we can deal with Microsoft excel in Python­­­­­­­. Excel is very popular tool which we deal in our daily office life. Lots of problems can be solved with excel with very ease.

First we will see that how we can create excel file from Python. There are so many libraries in Python which we can use for excel like openpyxl, xlsxwriter, xlrd, xlwt, xlutils. You can use any of these python excel library.

We will use here openpyxl for reading and writing excel through Python.

How to Install openpyxl?

Openpyxl is Python libraray, if you don’t have openpyxl libraray you can download it via pip.

pip install openpyxl

Above command will install openpyxl into your system.

Now open your favorite Python Editor to hands-on on Python excel programs.

How to create Excel file from Python?

Step 1: First we create excel file from Python. To use openpyxl library, we need to import it in our program.

from openpyxl import Workbook

we imported Workbook from openpyxl, as excel is called Workbook, inside Workbook we have worksheets like  sheet1, sheet2.. etc.

Step2: create object of Workbook.

After import Workbook, we will create object of Workbook.

wb=new Workbook()

Step 3: Save the Workbook.

Now we have created workbook object in step 2, its time to save the file in some location

# Save the excel file'D:\\Python\\test.xlsx')

we are going to save excel file inside python folder in D drive in window with filename =”test.xlsx”.

so overall program, is

#Python excel simple program
#import Workbook
from openpyxl import Workbook
wb = Workbook()

# Save the excel file'D:\\Python\\test.xlsx')

Above program will create empty excel file in specified location.