xlwt写入单元格,xlrd读出单元格

依次写入多个单元格

#-*- encoding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import xlwt;
import xlrd;
from xlutils.copy import copy;

for i in range(100):
    oldWb = xlrd.open_workbook("./filename.xls", formatting_info=True);
    oldWbS = oldWb.sheet_by_index(0)
    newWb = copy(oldWb);
    newWs = newWb.get_sheet(0);
    inserColNo = 0
    newWs.write(i,inserColNo,  "value"+str(i));
    newWb.save('filename.xls');


写入一列:

    lists=[]
    workbook=xlwt.Workbook() 
    st=workbook.add_sheet(u'sheet1',cell_overwrite_ok=True) 
    for index,item in enumerate(res):#先拼接成一个list
        lists.append(item['XXXXXX'])
    for index,item in enumerate(lists):
        st.write(index,0,item)
    path='./'+name+'.xls'  
    workbook.save(path)  

读取单元格

import xlrd
data = xlrd.open_workbook("./a.xls")
table = data.sheet_by_name(u'Sheet1')
result=table.cell(0,0).value#(0,0)也就是excel的最左上方的那个单元格


读出一列:

file_path='XXXXXXXXXx'
dataset=xlrd.open_workbook(file_path)
table=data.sheet_by_name(u'Sheet1') 
content=table.col_values(0)


你可能感兴趣的:(Python自然语言处理)