# Working with Excel Files in Python

Python is great programming language for manipulating the excel files. Excel files are mainly comprised of the
Workbook , Worksheet and Cells  with Rows and Columns.

Python provides a famous module known as the “openpyxl” for working with Excel. Lets explore this module by working with a demo project.

Introduction of the “openpyxl” module
First we have to understand the basics of the Excel components like what is the workbook , worksheet , Cell etc.
Workbook – An Excel spread sheet document is called a workbook
Worksheet – Each workbook can contain multiple sheets(also called worksheets).
Cell – A box at a particular column and row is called a cell. Each cell can contain a number or text value.
Lets install the “Openpyxl”.

[code] pip install –U openpyxl [/code]

Qucik Steps:-

a)Import the openpyxl module.Call the openpyxl.load_workbook() function.
b)Get a Workbook object.Call the get_active_sheet() or get_sheet_by_name() workbook method.
c)Get a Worksheet object.Use indexing or the cell() sheet method with row and column keyword arguments.
d)Get a Cell object.Read the Cell object’s value attribute.
e) Manipulate Cells.

Opening the Excel File

To open the Excel File you need to import the “openpyxl” into your program and call the Function load_workbook.

Get the Sheet Name and Printing them.

[python]sheetNames = workbookObject.get_sheet_names() print(sheetNames)
sheet = workbookObject.get_sheet_by_name(sheetNames)[/python]

Lets get the Rows and Columns used

[python]print(sheet.get_highest_row())
print(sheet.get_highest_column())[/python]

Printing Content of Excel File

Now lets go through all rows and column and see what all data Excel Have

[python]
for rowOfCellObj in sheet[‘A1′:’C3’]:
for cellObj in rowOfCellObj:
print(cellObj.coordinate, cellObj.value)
print(‘— ROW END —‘)
[/python]

output :
A1 2015 13
B1 App
C1 73
— ROW END —
A2 2014 03
B2 Mob
C2 85
— ROW END —

Full Code

[python]
import openpyxl

def main():
sheetNames = workbookObject.get_sheet_names()
print(sheetNames)
sheet = workbookObject.get_sheet_by_name(sheetNames)
print(sheet.get_highest_row())
print(sheet.get_highest_column())
##print(sheet)

for rowOfCellObj in sheet[‘A1′:’C3’]:
for cellObj in rowOfCellObj:
print(cellObj.coordinate, cellObj.value)
print(‘— ROW END —‘)

if __name__ == "__main__":
main()

[/python]

We will discuss more about excel in next blog post.

This site uses Akismet to reduce spam. Learn how your comment data is processed.
{"ft_lang_label":"__label__en","ft_lang_prob":0.64617115,"math_prob":0.6070109,"size":2503,"snap":"2022-27-2022-33","text_gpt3_token_len":629,"char_repetition_ratio":0.15686275,"word_repetition_ratio":0.06646526,"special_character_ratio":0.23971234,"punctuation_ratio":0.12619048,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9587223,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-30T19:24:09Z\",\"WARC-Record-ID\":\"<urn:uuid:cd1d5852-3efe-4166-ac55-9c108fbc7759>\",\"Content-Length\":\"104218\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:574deea0-59d7-404f-9352-1803afa00a24>\",\"WARC-Concurrent-To\":\"<urn:uuid:70c27066-afc8-4393-aeb3-9ca7323bb658>\",\"WARC-IP-Address\":\"194.1.147.17\",\"WARC-Target-URI\":\"https://scrolltest.com/2015/05/07/working-with-excel-files-in-python/\",\"WARC-Payload-Digest\":\"sha1:F2RRSAWNU3OHTWGOBPPICT5EN2ONXSOK\",\"WARC-Block-Digest\":\"sha1:FZAYLPQYTHNRI5FNN5JLJZY56T62HPGJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103877410.46_warc_CC-MAIN-20220630183616-20220630213616-00612.warc.gz\"}"}