import xlwt
时间: 2023-10-19 22:06:07 浏览: 76
import xlwt is a Python library that allows you to create Excel files in the .xls format. It provides various functions and classes to write data to an Excel file.
Here's an example of how you can use xlwt to create an Excel file and write data to it:
```python
import xlwt
# Create a new workbook and add a sheet
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('Sheet1')
# Write data to the sheet
sheet.write(0, 0, 'Hello')
sheet.write(0, 1, 'World')
# Save the workbook
workbook.save('output.xls')
```
In this example, we import the `xlwt` module and create a new workbook using `xlwt.Workbook()`. We then add a sheet to the workbook using `workbook.add_sheet('Sheet1')`.
Next, we write data to the sheet using the `sheet.write(row, col, data)` method. In this case, we write "Hello" to cell A1 (row 0, col 0) and "World" to cell B1 (row 0, col 1).
Finally, we save the workbook using `workbook.save('output.xls')`. This will create a new Excel file named "output.xls" in the current directory with the data we wrote.
Note that xlwt only supports the old .xls format, not the newer .xlsx format. If you need to work with .xlsx files, you can use the openpyxl or xlsxwriter libraries instead.
阅读全文