banner



How To Modify Background Color Of A Xls File In Python Xlrd

python read excel with xlrd

How to use xlrd library to read excel file

Python xlrd is a very useful library when you are dealing with some older version of the excel files (.xls). In this tutorial, I will share with you how to use this library to read information from .xls file.

Let's become started with xlrd

You lot volition need to install this library with below pip command:

and import the library to your source code:

To open your excel file, yous will need to pass in the full path of your file into the open_workbook function.It returns the workbook object, and in the next line you volition be able to access the canvass in the opened workbook.

workbook = xlrd.open_workbook(r"c:\exam.xls")

At that place are multiple ways for doing it, you can admission by sheet proper name, sail index, or loop through all the sheets

canvass = workbook.sheet_by_name("Sail") #getting the outset sheet sheet_1 = workbook.sheet_by_index(0)  for sh in workbook.sheets():     print(sh.name)

To get the number of rows and columns in the sheet, you can access the post-obit attributes. By default,
all the rows are padded out with empty cells to brand them same size, merely in case yous desire to ignore the
empty columns at the cease, you may consider ragged_rows parameter when you call the open_workbook function.

row_count = sheet.nrows col_count = canvas.ncols # use canvass.row_len() to get the constructive column length when yous set ragged_rows = True

With number of rows and columns, yous will be able to access the data of each of the cells

for cur_row in range(0, row_count):     for cur_col in range(0, col_count):         prison cell = sheet.cell(cur_row, cur_col)         print(cell.value, cell.ctype)

Instead of accessing the information cell by cell, you can also access information technology by row or by column, due east.g. assume your first row is the column header, you lot can get all the headers into a list as below:

header = sheet.row_values(0, start_colx=0, end_colx=None) # row_slice returns the cell object(both data type and value) in case you lot also need to check the data type #row_1 = sail.row_slice(1, start_colx=0, end_colx=None)

Go the whole column values into a list:

col_a = sheet.col_values(0, start_rowx=0, end_rowx=None) # col_slice returns the cell object of the specified range col_a = sail.col_slice(0, start_rowx=0, end_rowx=None)

In that location is a quite common fault when handling the xls files, please check this article for fixing the CompDocError.

Conclusion

xlrd is a clean and easy to use library for handling xls files, but unfortunately there is no active maintenance for this library every bit Excel already evolved to xlsx format. There are other libraries such as openpyxl which can handle xlsx files very well for both data and cell formatting. I would suggest yous to employ xlsx file format in your new project whenever possible, so that more active libraries are supported.

If yous would like to sympathize more than about openpyxl , please read my next article almost this library.

As per e'er, welcome to any comments or questions. Thanks.

You may also similar

Would love your thoughts, please annotate.x

Source: https://www.codeforests.com/2020/05/25/use-xlrd-library-to-read-excel-file/

Posted by: hillcalmsen.blogspot.com

0 Response to "How To Modify Background Color Of A Xls File In Python Xlrd"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel