In this article we want to learn about Create Excel Files with Python: A Guide to Using xlwt for .xls Format. so Python is popular programming language that can be used for wide range of tasks, including working with Excel files. One popular library for writing data to Excel files in the older .xls format is xlwt. In this article we are going to explore what xlwt is, how to install it and some examples of how to use it in Python.
What is xlwt?
xlwt is Python library for writing data to Excel files in the older .xls format. It was created by John Machin and is currently maintained by the Python Packaging Authority. xlwt allows you to create new Excel files, write data to them and control formatting. in this article we want to Create Excel Files with Python: A Guide to Using xlwt for .xls Format.
How to Install xlwt ?
Installing xlwt is simple process. You can use pip package installer for Python to install the library. Open your terminal or command prompt and type the following command:
1 |
pip install xlwt |
By this command you can install xlwt.
Examples of Using xlwt in Python
Once you have installed xlwt you can begin using it in your Python projects. These are some examples of how to use xlwt to write data to Excel file.
1. Creating a New Workbook
First of all in using xlwt is to create new Excel file. You can do this by using Workbook() function. This is an example:
1 2 3 4 |
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('Sheet 1') |
This code creates a new Excel file with one worksheet and store it in the workbook
variable.
2. Writing Data to a Cell
When you have created the worksheet, you can begin writing data to it. you can write string, number, or formula to cell using the write() method. This is an example:
1 |
worksheet.write(0, 0, 'Hello, world!') |
This code will write the string ‘Hello, world!’ to the cell in the first row and first column of the worksheet.
3. Setting Cell Formatting
also xlwt allows you to control cell formatting, such as font, alignment, and background color. This is an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
style = xlwt.XFStyle() font = xlwt.Font() font.bold = True style.font = font alignment = xlwt.Alignment() alignment.horz = xlwt.Alignment.HORZ_CENTER style.alignment = alignment pattern = xlwt.Pattern() pattern.pattern = xlwt.Pattern.SOLID_PATTERN pattern.pattern_fore_colour = xlwt.Style.colour_map['gray25'] style.pattern = pattern worksheet.write(0, 0, 'Hello, world!', style) |
This code will write string ‘Hello, world!’ to the cell in the first row and first column of the worksheet with bold font, centered alignment, and a gray background.
4. Saving the Workbook
when you have finished writing data to worksheet you can save the Excel file using the save() method. this is an example:
1 |
workbook.save('example.xls') |
This code will save the Excel file with the name ‘example.xls’ in the current directory.
Learn More on Python
- Python-Docx: Creating Microsoft Word Documents with Python
- Python and Microsoft Word: A Beginner’s Guide to Automating Documents
- How to Install docx2python: Python Library for Word Documents
- Merge Microsoft Word Documents with Python Docxcompose
- Mastering Excel Manipulation with Python Openpyxl
- Python Treq: An Introduction to a Powerful HTTP Client Library
- Introduction to Python httplib2 Library
- An Introduction to Python’s urllib Library
- Python httpx: A High-Performance HTTP Client for Python 3
Final Thoughts
xlwt is powerful library for working with Excel files in Python. It allows you to create new Excel files, write data to them and control formatting which can be useful for wide range of applications. By using xlwt in your Python projects, you can make working with Excel files easier and more efficient.