Simple Example of pyhon reading and writing Excel

Source: Internet
Author: User

I. Prerequisites
1 install PIP: sudo apt-Get install Python-pip
2. Install the python xlrd module: sudo Pip install xlrd
3. Install Python's pyexcelerator module: sudo Pip install pyexcelerator
Ii. directory structure
1 static directory
3 readexcel. py
4 writeexcel. py

Iii. readexcel. py content

#! Usr/bin/python <! -- LANG: Python --> #-*-coding: UTF-8-*-<! -- LANG: Python -- >__ author _ = 'suiyc' <! -- LANG: Python --> <! -- LANG: Python --> Import OS <! -- LANG: Python --> Import xlrd <! -- LANG: Python --> <! -- LANG: Python --> static_path = OS. Path. Join (OS. Path. dirname (_ file _), "static") # Set the static path <! -- LANG: Python --> file_name = "% S/sida.xls" % static_path <! -- LANG: Python --> <! -- LANG: Python --> BK = xlrd. open_workbook (file_name) <! -- LANG: Python --> shxrange = range (BK. nsheets) <! -- LANG: Python --> <! -- LANG: Python --> try: <! -- LANG: Python --> Sh = BK. sheet_by_name ('sheet1') <! -- LANG: Python --> example T: <! -- LANG: Python --> Print 'no sheet in % s named sheet1' % file_name <! -- LANG: Python --> <! -- LANG: Python --> # Get rows num <! -- LANG: Python --> nrows = Sh. nrows <! -- LANG: Python --> # Get Colum num <! -- LANG: Python --> ncols = Sh. ncols <! -- LANG: Python --> <! -- LANG: Python --> Print "nrows: % d, ncols: % d" % (nrows, ncols) <! -- LANG: Python --> <! -- LANG: Python --> # Read the first row and the first Colum data <! -- LANG: Python --> cell_value = Sh. cell_value (0, 0) <! -- LANG: Python --> Print cell_value <! -- LANG: Python --> <! -- LANG: Python --> app_list = [] <! -- LANG: Python --> # Get all of the third Colum data records t the fire row <! -- LANG: Python --> for I in range (1, nrows): <! -- LANG: Python --> app_id = int (Sh. cell_value (I, 2) <! -- LANG: Python --> app_list.append (app_id) <! -- LANG: Python --> <! -- LANG: Python --> Print app_list <! -- LANG: Python --> Print 'total: % d' % Len (app_list)

Iv. writeexcel. py content display

#! Usr/bin/python <! -- LANG: Python --> #-*-coding: UTF-8-*-<! -- LANG: Python -- >__ author _ = 'suiyc' <! -- LANG: Python --> <! -- LANG: Python --> Import OS <! -- LANG: Python --> from pyexcelerator import * <! -- LANG: Python --> <! -- LANG: Python --> static_path = OS. Path. Join (OS. Path. dirname (_ file _), "static") # Set the static path <! -- LANG: Python --> file_name = "% S/test.xls" % static_path <! -- LANG: Python --> <! -- LANG: Python --> W = Workbook () # create a workbook <! -- LANG: Python --> Ws = W. add_sheet ('Hey, haders') # create a worksheet <! -- LANG: Python --> ws. Write (1st, 'name') # write name in column 1st of row <! -- LANG: Python --> ws. Write (1st, 'age') # Write age in Column 2nd of row <! -- LANG: Python --> ws. Write (1st, 'gender') # Write gender in Column 2nd of row <! -- LANG: Python --> ws. Write (2nd, 'kimi') # Write Kimi in columns 1st of Row 3 <! -- LANG: Python --> ws. Write (2nd, '20') # Write 20 in Column 2nd of row <! -- LANG: Python --> ws. Write (2nd, 'male') # Write male in Column 2nd of row <! -- LANG: Python --> W. Save (file_name)

Simple Example of pyhon reading and writing 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.