In the process of learning Python, we will encounter Excel reading and writing problems. By searching, we can use the XLWT module to write data to an Excel table and use the XLRD module to read data from Excel. Here's how to implement read and write to Excel using Python.
(1) Write operations to Excel:
#-*-coding:utf-8-*-#导入xlwt模块import xlwt# Create a Workbook object, This is equivalent to creating an Excel file Book = XLWT. Workbook (encoding= ' utf-8 ', style_compression=0) ' Workbook class is initialized with encoding and style_compression parameters encoding: Set character encoding, In general, set this: W = Workbook (encoding= ' utf-8 '), you can output Chinese in Excel. The default is ASCII. Of course remember to add the file header: #!/usr/bin/env python#-*-coding:utf-8-*-style_compression: Indicates whether compression is not used. #创建一个sheet对象, a sheet object corresponds to a table in an Excel file. # Right-click on the desktop to create a new Excel file that contains sheet1,sheet2,sheet3 three sheets sheet = book.add_sheet (' Test ', cell_overwrite_ok=true) # The test is the name of this table, CELL_OVERWRITE_OK, indicates whether the cell can be overwritten, in fact, is a parameter worksheet instantiation, the default value is false# to the table test to add data sheet.write (0, 0, ' Englishname ') # where ' 0-row, 0-column ' specifies the cell in the table, ' Englishname ' is the content written to the unit Sheet.write (1, 0, ' Marcovaldo ') txt1 = ' Chinese name ' sheet.write (0 , 1, Txt1.decode (' Utf-8 ')) # Here you need to decode the Chinese string into Unicode code, otherwise you will get an error txt2 = ' Machovado ' sheet.write (1, 1, txt2.decode (' Utf-8 ')) # Finally, Save the above operation to the specified Excel file Book.save (R ' E:\test1.xls ') # in front of the string with R, declared as the raw string, so that the escape is not handled. Otherwise, an error may be
After the completion of the computer under the E disk will produce Test1.xls files, into:
(2) Write operations to Excel
1, the contents of the table are as follows:
2.
#-*-Coding:utf-8-*-import xlrdxlsfile = r "C:\Users\Administrator\Desktop\test\Account.xls" # Open XLS file in specified path book = xlrd . Open_workbook (Xlsfile) #得到Excel文件的book对象, instantiating object sheet0 = Book.sheet_by_index (0) # Get Sheet object by sheet index print "1,", Sheet0sheet_name = Book.sheet_names () [0]# Gets the sheet table name of the specified index print "2,", Sheet_namesheet1 = Book.sheet_by_name (sheet_name # by sheet name to get, of course, if you know sheet name can be directly specified nrows = sheet0.nrows # Get the total number of rows print "3,", nrows# loop print the contents of each line for I in range (nrows): C1/>print sheet1.row_values (i) Ncols = Sheet0.ncols #获取列总数print "4,", Ncolsrow_data = sheet0.row_values (0) # Get the data list for line 1th print row_datacol_data = sheet0.col_values (0) # Get the data list for column 1th print "5," and col_data# read the data in the table by coordinates cell_value1 = Sheet0.cell_value (0, 0) print "6,", cell_value1cell_value2 = Sheet0.cell_value (0, 1) print "7,", Cell_value2
3, the result of implementation:
Resources:
http://blog.csdn.net/majordong100/article/details/50708365
Http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html
Http://www.cnblogs.com/snake-hand/p/3153158.html
python-reading and writing to Excel