Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write excel files by python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how python reads and writes excel files. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Read and write excel files

Microsoft Excel is a spreadsheet software written by Microsoft for computers using Windows and Apple Macintosh operating systems. The intuitive interface, excellent computing capabilities and charting tools, coupled with successful marketing, make Excel the most popular personal computer data processing software.

How to use xlrd

Import xlrd# opens the excel file Create a workbook object, the book object is the s11.xlsx file, the table contains the sheetname rbook = xlrd.open_workbook ('/ Users/yuchao/s11.xlsx') # sheets method returns a list of objects, [] rbook.sheets () rsheet=rbook.sheet_by_index (0) # number of rows=rsheet.nrows# access columns cols=rsheet.ncolsprint ('number of rows:', number of rows,' columns' Cols) # get the cell value cell=rsheet.cell (0J0) print through the position coordinates of cell (the value of '0J0 coordinate is:', cell.value) # get the value of the second line, and the parameter is (number of rows) Start, end) row1=rsheet.row_values (1) print ('the value of the first row is:', row1)

Xlwt modifies excel

#-*-coding:utf-8-*-# Author: yuchao# Data: 2018-7-18 16:08import xlrd, xlwtrbook = xlrd.open_workbook ('/ Users/yuchao/s11.xlsx') rsheet = rbook.sheet_by_index (0) # get the number of sheet object columns nc = rsheet.ncols# write a piece of data rsheet.put_cell (0, nc, xlrd.XL_CELL_TEXT, 'total score, None) # the number of rows for row in range (1) traverse the data Rsheet.nrows): # summing each row of data t = sum (rsheet.row_values (row, 1)) rsheet.put_cell (row, nc, xlrd.XL_CELL_NUMBER, t, None) # create a document object wbook = xlwt.Workbook () wsheet = wbook.add_sheet (rsheet.name) # set style style = xlwt.easyxf ('align: vertical center Horizontal center') # iterate through each row of for r in range (rsheet.nrows): # each column of for c in range (rsheet.ncols): wsheet.write (style c), style) wbook.save ('/ Users/yuchao/s11_bak.xlsx')

Read excel

Import xlrdfrom xlrd.book import Bookfrom xlrd.sheet import Sheetfrom xlrd.sheet import Cellworkbook = xlrd.open_workbook ('/ Users/yuchao/s11.xlsx') sheet_names = workbook.sheet_names () # sheet = workbook.sheet_by_name ('worksheet 1') sheet = workbook.sheet_by_index (1) # all lines of the circular Excel file for row in sheet.get_rows (): # all columns of a circular line for col in row: # obtained Take the value print (col.value) in a cell, and that's all for the article on "how python reads and writes excel files". Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report