Import XLWT
# book = XLWT. Workbook () # Create a new Excel
# sheet = book.add_sheet (' Sheet1 ') # Add a sheet page
# sheet.write (0, 0, ' name ')
# sheet.write ( 0, 1, ' sex ')
# sheet.write (0, 2, ' age ')
# book.save (' Stu.xls ') # Microsoft Office cannot end with xlsx, wps random
title = [' Name ', ' age ', ' sex Don't ', ' fractions ']
Stus = [[' Mary ', 20, ' female ', ' all ', ' Mary ', 20, ' female ', 89.9], [' Mary ', 20, ' female ', 89.9], [' Mary ', 20, ' female ', 89.9]]
Book = XLWT. Workbook () # Create a new Excel
Sheet = book.add_sheet (' Sheet1 ') # Add a sheet page
cols = 0
for T in title:
Sheet.writ E (0, cols, t)
cols + = 1
row = 1 # control line
for Stu in Stus:
New_cols = 0
for s in Stu: # Write each column
Sheet.write (Row, New_cols, s)
New_cols + = 1
Row + = 1
book.save (' Stu1.xls ')
Import xlrd
Book = Xlrd.open_workbook (' Stu1.xls ') # Open an Excel
Sheet = book.sheet_by_index (0) # get sheet page in order
# Sheet1 = book.sheet_by_name (' Sheet1 ') # obtained based on sheet page name
# print (Sheet.cell (0, 0). Value) # Specify row and column fetch data
# print (Sheet.cell (0, 1). Value)
# print (Sheet.cell (0, 2). Value)
Print (Sheet.ncols) # Gets how many columns are in Excel
Print (sheet.nrows) # Gets how many lines are in Excel
For I in Range (sheet.nrows):
Print (Sheet.row_values (i)) # Take the first few rows of data
Print (sheet.col_values (0)) # Take the first few columns of data
From xlutils.copy Import copy
Import xlrd
Book1 = Xlrd.open_workbook (' Stu1.xls ')
Book2 = Copy (Book1) # Copies a copy of the original
Sheet = book2.get_sheet (0) # Get the first few sheet pages
Sheet.write (1, 3, 0)
Book2.save (' Stu1.xls ')
python--manipulating Excel