Read and Write excel in Python

Source: Internet
Author: User
Read and Write excel in Python
10:02:21 let me say a few words
Add to favorites I want to contribute

Read and Write excel in Python
Because I am tired of manual operations on Excel tables, I took the time to learn how to operate Python Excel tables. Okay, I admit I'm lazy ......
Module installation:
Install setup. py in the xlrd and xlwt files respectively. The installation command is setup. py install.
Go to the python interpreter and enter import xlwt. Normally, no error is reported.
The following is an exchange program that reads data from the excle file to normal text and writes data from normal text to excel:
1 # encoding: utf8
2 Import xlrd
3 Import xlwt
4
5 class operexcel ():
6 # Read an Excel table
7 def rexcel (self, inefile, OUTFILE ):
8 rfile = xlrd. open_workbook (inefile)
9 # create an index to obtain a worksheet
10 table = rfile. sheet_by_index (0)
11 # Other Methods
12 # Table = rfile. Sheets () [0]
13 # Table = rfile. sheet_by_name (U 'sheet1 ')
14
15 # obtain the value of the entire row and column
16 table. row_values (0)
17 table. col_values (0)
18
19 # obtain the number of rows and columns
20 nrows = table. nrows-1
21 ncols = table. ncols
22
23 # obtain the list data cyclically
24 # For I in range (nrows ):
25 # print table. row_values (I)
26 wfile = open (OUTFILE, 'w ')
27 # retrieve all values in the first column
28 For I in range (nrows ):
29 # table. Cell (I, 0). Value to obtain the value of a cell
30 wfile. Write (table. Cell (I, 0). value. encode ('utf8') + '\ n ')
31 wfile. Close ()
32
33 # write data into an Excel table
34 def wexcel (self, infile, outefile ):
35 rfile = open (infile, 'R ')
36 Buf = rfile. Read (). Split ('\ n ')
37 rfile. Close ()
38
39 W = xlwt. Workbook ()
40 sheet = W. add_sheet ('sheet1 ')
41 for I in range (LEN (BUF )):
42 print Buf [I]
43 sheet. Write (I, 0, Buf [I]. Decode ('utf8 '))
44 W. Save (outefile)
45
46 if _ name _ = '_ main __':
47 t = operexcel ()
48 t.rexcel('test.xls ', 'test ')
49 t.wexcel('test', '1.xls ')
50

Related Article

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.