Python third party library xlrd and xlwt installation and reading and writing Excel forms

  • 2020-05-24 05:45:56
  • OfStack

preface

I believe that everyone should have some experience. In normal times, we often encounter the situation of processing Excel table data, which is too troublesome to handle manually. We can use Python to solve this problem, and we need two Python extensions, xlrd and xlwt.

xlrd and xlwt Python third party libraries, so is the need to install, can be in python website https: / / pypi python. org/pypi download the module to install, also can through other means, such as easy_install or pip, take a look at the detailed installation method is introduced with the read/write Excel form.

Write Excel data using xlwt

How to install xlwt


$ sudo pip install xlrd

The sample code


import xlwt

xls = xlwt.Workbook()
sheet = xls.add_sheet('sample')
sheet.write(0, 0, 'netcon')
sheet.write(0, 1, 'conw.net')
xls.save('sample.xls')

This is the simplest example. Create a Excel table, create a new sheet named sample, and write jb51, ofstack.com in A1 and B1.

Read Excel data using xlrd

Installation method of xlrd


$ sudo pip install xlrd

The sample code


import xlrd

xls = xlrd.open_workbook('sample.xls')
sheet = xls.sheets()[0]
values = sheet.row_values(0)
print(values)

This code USES xlrd to read the Excel table created above, and the output is:


['jb51', 'ofstack.com']

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: