加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python 对xls写入信息

发布时间:2020-12-20 10:21:39 所属栏目:Python 来源:网络整理
导读:只能新创建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 列

只能新创建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,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读