Use Third-party library xlrd in Python to write an Excel file sample _python

Source: Internet
Author: User

Following the previous article using XLRD to read Excel, this article is about how to write Excel, write Excel we need to use the Third-party library XLWT, and XLRD, Xlrd said Read XLS,XLWT represents write xls, Also, the current version supports only version 97-03 of Excel. XLWT Download: XLWT 0.7.4

Install XLWT

The way you install it is Python setup.py install, or you can extract it directly into your engineering directory.

API Introduction

Get an XLS instance

Copy Code code as follows:

xls = Excelwrite.workbook ()

Add a sheet
Copy Code code as follows:

Sheet = Xls.add_sheet ("Sheet1")

Writing data to Sheet
Copy Code code as follows:

Sheet.write (Row_index, Col_index, value)

Save Build XLS
Copy Code code as follows:

Xls.save (file_name)

using XLWT
Copy Code code as follows:

#-*-Coding:utf-8-*-
'''
Created on 2012-12-14

@author: walfred
@module: Xlrdpkg.write
@description:
'''

Import XLWT as Excelwrite

def writexls (file_name):
value = [[' Name ', ' Jim, "Hmm", "Lilei"], ["Sex", "man", "Woman", "Mans"], ["Age", ","], ["Country", "USA", "CHN", " CHN "]]
xls = Excelwrite.workbook ()
Sheet = Xls.add_sheet ("Sheet1")

For I in range (0, 4):
For j in range (0, Len (value)):
Sheet.write (J, I, Value[i][j])

Xls.save (file_name)

if __name__ = = "__main__":
Writexls ("./test_write.xls");

Of course, this is simply the introduction of how to use the XLWT, the more versatile API requires the reader to learn from the download package, such as formatting, hyperlinks, formulas and so on.

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.