python 对xls写入信息

只能新创建xls

# coding=utf-8
import xlwt

writebook = xlwt.Workbook()                #打开excel

test= writebook.add_sheet('test')          #添加一个名字叫test的sheet

test.write(0,1,'this is a test')           #第0行第1列写入字符串'this is a test'

writebook.save('testdata.xls')             #一定要保存为xls,后缀是xlsx的文件打不开

 后记: 

要循环才能连续插入 不然只会插入最后一句

追加xls内容

首先要安装三个模块:xlrd,xlwt,xlutils

命令:pip install xlrd xlwt xlutils

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from xlrd import open_workbook
from xlutils.copy import copy

r_xls = open_workbook("test.xls") # 读取excel文件
row = r_xls.sheets()[0].nrows # 获取已有的行数
excel = copy(r_xls) # 将xlrd的对象转化为xlwt的对象
table = excel.get_sheet(0) # 获取要操作的sheet

#对excel表追加一行内容
table.write(row, 0, u'内容1') #括号内分别为行数、列数、内容
table.write(row, 1, u'内容2')
table.write(row, 2, u'内容3')

excel.save("test.xls") # 保存并覆盖文件

参考 https://www.cnblogs.com/zhenwei66/p/8406201.html

读取xls的方法: https://www.cnblogs.com/kaibindirver/p/9917158.html

原文地址:https://www.cnblogs.com/kaibindirver/p/11645127.html