Python Operations Excel

Source: Internet
Author: User

Python operations Excel uses XLRD, XLWT, and xlutils modules. XLRD module is read in Excel, XLWT module is written in Excel, Xlutils is used to modify Excel's

One, Python read Excel

Import xlrd
Book = Xlrd.open_workbook (' All_stu.xls ')#Open aExcel
Sheet = Book.sheet_by_index (0)#Get By orderSheet
# Sheet2 = Book.sheet_by_name (' Sheet1 ') #UnderSheetPage name getSheet
# Print (Sheet.cell (0,0). Value) #Specifying rows and columns for data
# print (sheet.ncols) #GetExcelHow many columns are there?
# print (sheet.nrows) #GetExcelHow many lines are inside?
Sheet.row_values (1) # Take the first few rows of data
Span style= "color: #808080; Font-style:italic "># Print (Sheet.col_values (1)) # Take the first few columns of data
Span style= "color: #000080; Font-weight:bold ">for i in range ( sheet.nrows): # Get excel How many lines
print (Sheet.row_values (i)) Span style= "color: #808080; Font-style:italic "># Take the first few rows of data

Second, Python operations Excel

Import XLWT
Stus = [
[‘Name‘,‘Age‘,‘Gender‘,‘Scores‘],
[' Mary ',20,‘Woman‘,89.9],
[' Mary ',20,‘Woman‘,89.9],
[' Mary ',20,‘Woman‘,89.9],
[' Mary ',20,‘Woman‘,89.9]
]
Book = XLWT. Workbook ()#Create a newExcel
Sheet = Book.add_sheet (' Sheet1 ')#Add aSheetPage
Sheet.write (0,0,‘Name‘)#Write a line line
# sheet.write (0,1, 'Gender‘)
# sheet.write (0,2, 'Age‘)
# book.save (' Stu.xls ') #Microsoft'sOfficeCan't usexlsxEnd OF,WpsRandom
Raw =0#of the control line
For StuIn Stus:#Looping writes
Col =0# control columns, which do not loop once, are written from the first column to
for s in stu:
Sheet.write (raw,col,s)
Col+=1
Raw+=1
Book.save (" Kkk.xls ') # Microsoft's office not Available xlsx end, Span style= "color: #808080; Font-style:italic ">wps casual

Third, Python modifies Excel

From Xlutils.copyImport Copy
Import xlrd
Book1 = Xlrd.open_workbook (' Stu.xls ')
Book2 = Copy (Book1)#A copy of the originalExcel
Sheet = Book2.get_sheet (0) # Get the first few sheet pages
Sheet.write (1,3,0)# Write the rows, columns, and modified values that need to be modified
Sheet.write (1,0,' little Black ')
Book2.save (' Stu.xls ')

Python Action 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.