添加链接描述
1、打开excel
readbook = xlrd.open_workbook(r’\test\canying.xlsx’)
2、获取读入的文件的sheet
sheet = readbook.sheet_by_index(1)#索引的方式,从0开始
sheet = readbook.sheet_by_name(‘sheet2’)#名字的方式
3、获取sheet的最大行数和列数
nrows = sheet.nrows#行
ncols = sheet.ncols#列
4、获取某个单元格的值
lng = table.cell(i,3).value#获取i行3列的表格值
lat = table.cell(i,4).value#获取i行4列的表格值
5、打开将写的表并添加sheet
writebook = xlwt.Workbook()#打开一个excel
sheet = writebook.add_sheet(‘test’)#在打开的excel中添加一个sheet
6、将数据写入excel
sheet.write(i,0,result[0])#写入excel,i行0列
sheet.write(i,1,result[1])
7、保存
writebook.save(‘answer.xls’)#一定要记得保存
coding=utf-8
import xlrd
import xlwt
import datetime
import os
lass excelProcess:
def init(self,keywordExcelFile,mainExcelFile):
self.keywordExcelFile = keywordExcelFile
self.mainExcelFile = mainExcelFile
def WriteLog(self, message,date):
fileName = os.path.join(os.getcwd(), date + ‘.txt’)
with open(fileName, ‘a’) as f:
f.write(message)
def WriteSheetRow(self,sheet, rowValueList, rowIndex, isBold):
i = 0
style = xlwt.easyxf(‘font: bold 1’)
# style = xlwt.easyxf(‘font: bold 0, color red;’)#红色字体
# style2 = xlwt.easyxf(‘pattern: pattern solid, fore_colour yellow; font: bold on;’) # 设置Excel单元格的背景色为黄色,字体为粗体
for svalue in rowValueList:
if isBold:
sheet.write(rowIndex, i, svalue, style)
else:
sheet.write(rowIndex, i, svalue)
i = i + 1
def save_Excel(self):
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet1', cell_overwrite_ok=True)
headList = ['IR_SITENAME', 'IR_AUTHORS', 'SY_INFOTYPE', 'RID', 'IR_URLTITLE','SY_KEYWORDS',
'IR_URLNAME', 'IR_URLTIME',
'IR_GROUPNAME', 'IR_CHANNEL',
'SY_BB_COMMON', 'summary', 'keyword'
]
rowIndex = 0
self.WriteSheetRow(sheet, headList, rowIndex, True)
for i in range(1, 11):
rowIndex = rowIndex + 1
valueList = []
for j in range(1, 14):
valueList.append(j * i)
self.WriteSheetRow(sheet, valueList, rowIndex, False)
fileName = os.path.join(os.getcwd(),‘test.xlsx’)
wbk.save(fileName)