使用Python写入 Excel 工作表
使用xlwt
模块,可以对电子表格执行多项操作。例如,可以在Python中编写或修改数据。此外,用户可能必须浏览各种工作表并根据某些标准检索数据或修改某些行和列并做很多工作。
让我们看看如何使用Python创建和写入 excel-sheet。
代码#1:
# Writing to an excel
# sheet using Python
import xlwt
from xlwt import Workbook
# Workbook is created
wb = Workbook()
# add_sheet is used to create sheet.
sheet1 = wb.add_sheet('Sheet 1')
sheet1.write(1, 0, 'ISBT DEHRADUN')
sheet1.write(2, 0, 'SHASTRADHARA')
sheet1.write(3, 0, 'CLEMEN TOWN')
sheet1.write(4, 0, 'RAJPUR ROAD')
sheet1.write(5, 0, 'CLOCK TOWER')
sheet1.write(0, 1, 'ISBT DEHRADUN')
sheet1.write(0, 2, 'SHASTRADHARA')
sheet1.write(0, 3, 'CLEMEN TOWN')
sheet1.write(0, 4, 'RAJPUR ROAD')
sheet1.write(0, 5, 'CLOCK TOWER')
wb.save('xlwt example.xls')
输出 :
代码 #2:在 excel 中添加样式表
# importing xlwt module
import xlwt
workbook = xlwt.Workbook()
sheet = workbook.add_sheet("Sheet Name")
# Specifying style
style = xlwt.easyxf('font: bold 1')
# Specifying column
sheet.write(0, 0, 'SAMPLE', style)
workbook.save("sample.xls")
输出 :
代码 #3:向单元格添加多种样式
# importing xlwt module
import xlwt
workbook = xlwt.Workbook()
sheet = workbook.add_sheet("Sheet Name")
# Applying multiple styles
style = xlwt.easyxf('font: bold 1, color red;')
# Writing on specified sheet
sheet.write(0, 0, 'SAMPLE', style)
workbook.save("sample.xls")
输出 :