python--manipulating Excel

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.