EXCEL  写入数据保持原样式
import xlwt
import xlrd
import xlutils
import xlutils.copy
class ExcelHandle():
def __init__(self, filename):
self.filename = filename
self.test_data = xlrd.open_workbook(self.filename,formatting_info=True) # 保留原格式
self.sheet = self.test_data.sheet_by_index(0)
def writeExcel(self,row,col,value):
self.row = row
self.col = col
self.args = value
style = xlwt.XFStyle()
oldsheet = xlrd.open_workbook(self.filename,formatting_info=True)
newsheet = xlutils.copy.copy(oldsheet)
newwrite = newsheet.get_sheet(0)
newwrite.write(self.row, self.col, str(value), style)
newsheet.save(self.filename)
def readExcel(self, row,col):
comlist = str(self.sheet.cell_value(row, col))
return comlist if __name__ == '__main__':
t = ExcelHandle('bookname.xlsx') for i in range(2,15):
for j in range(1, 10):
X = t.readExcel(i,1)
print(X)
print("______________")
t.writeExcel(i,j,"sssss")

#coding=utf-8

import xlwt
import xlrd
import xlutils.copy #################################################################################
# Create a new sheet1
#workbook = xlwt.Workbook()
#sheet1 = workbook.add_sheet('sheet1',cell_overwrite_ok=True) # Write something into the sheet1
#sheet1.write(0,0,'connected IP')
#sheet1.write(0,1,'Addr IPV4 IP(s)') # save excel file
#workbook.save('C:\\Users\\32065\\Desktop\\Net\\simulation\\excel\\test.xls')
#print 'create excel file successfully!' ################################################################################
rb = xlrd.open_workbook('C:\\Users\\32065\\Desktop\\Net\\simulation\\excel\\test.xls')
wb = xlutils.copy.copy(rb)
#获取sheet对象,通过sheet_by_index()获取的sheet对象没有write()方法
ws = wb.get_sheet(0)
#写入数据
ws.write(0, 1, 'changed!')
ws.write(1, 0, '10-changed!')
ws.write(2, 0, '20-changed!')
#添加sheet页
# wb.add_sheet('sheetnnn2',cell_overwrite_ok=True)
#利用保存时同名覆盖达到修改excel文件的目的,注意未被修改的内容保持不变
wb.save('C:\\Users\\32065\\Desktop\\Net\\simulation\\excel\\test.xls') ############################################################################## # read the excel file
data = xlrd.open_workbook('C:\\Users\\32065\\Desktop\\Net\\simulation\\excel\\test.xls')
table = data.sheets()[0] # open the first sheet
nrows = table.nrows # not-null rows
print nrows
for i in range(nrows): # print by rows
if i == 0: # 跳过第一行
continue
print table.row_values(i)[0:2]

python 3 往Excel 中的写入内容但不覆盖原内容的更多相关文章

  1. Python批量修改Excel中的文件内容

    import osimport xlrdfrom xlutils.copy import copydef base_dir(filename=None):    return os.path.join ...

  2. 【Python】如何处理Excel中的数据

    我们平时在做自动化测试的时候,可能会涉及到从表格中去读取或者存储数据,我们除了可以使用openpyxl来操作excel,当然也可以利用pandas来完成,这篇随笔只是我在学习过程中的简单记录,其他的功 ...

  3. Python基础-修改excel中内容

    from xlutils.copy import copy import xlrd import os #1.打一要修改的excel #2.再打开另一个excel #3.把第一个excel里面修改东西 ...

  4. python - requests从excel中获取测试用例数据

    HttpRequests.py #-*- coding:utf-8 -*- import requests class HttpRequests(): def http_requests(self,u ...

  5. python实现操作excel,数据写入excel的一行或者一列

    # _*_ coding:utf-8 _*_ import random import xlwt,string class ImportData(object): def create_num(sel ...

  6. 使用excel中的数据快速生成sql语句

    在小公司的话,总是会有要开发去导入历史数据(数据从旧系统迁移到新系统上)的时候.这个时候,现场实施或客户会给你一份EXCEL文档,里面包含了一些别的系统上的历史数据,然后就让你导入到现在的系统上面去. ...

  7. Python中,添加写入数据到已经存在的Excel的xls文件,即打开excel文件,写入新数据

    背景 Python中,想要打开已经存在的excel的xls文件,然后在最后新的一行的数据. 折腾过程 1.找到了参考资料: writing to existing workbook using xlw ...

  8. 用python在excel中读取与生成随机数写入excel中

    今天是我第一次发博客,就关于python在excel中的应用作为我的第一篇吧. 具体要求是:在一份已知的excel表格中读取学生的学号与姓名,再将这些数据放到新的excel表中的第一列与第二列,最后再 ...

  9. Python+request 将获取的url和接口响应时间(timeout)写入到Excel中《八》

    使用场景: 在工作中,常见某个功能的查询,当查询关联表特别多时,开发写的SQL语句等等问题导致响应时间较慢,那么作为测试需要将每个接口的响应时间进行测试,对同个接口多次执行,并将测试结果写入到exce ...

随机推荐

  1. 【JVM】垃圾收集器

    程序计数器.Java虚拟机栈.本地方法栈分配的内存是确定的,生命周期与线程同样.所以不须要过多考虑回收问题. 而Java堆和方法区仅仅有运行时才知道有哪些对象被创建,须要多少内存,这部分的内存分配和回 ...

  2. shouldComponentUpdate 是做什么的,(react 性能优化是哪个周期函数?)

    shouldComponentUpdate询问组件是否需要更新的一个钩子函数,判断数据是否需要重新渲染,返回一个布尔值.默认的返回值是true,需要重新render().若如果返回值是false则不触 ...

  3. Mac下多版本JDK安装

    1.下载 JDK1.6官方未提供1.6的mac版本,需要去apple的开发者网站 JDK1.7.JDK1.8可以去Oracle官方下载 此处提供网盘下载地址: 链接: http://pan.baidu ...

  4. centos 环境搭建jenkins服务

    1.下载jenkins war包 https://jenkins.io/download/ 选择Generic Java package (.war)下载2.下载apache tomcat 8 htt ...

  5. centos6 利用外部的smpt服务器计划任务发送邮件

    centos可通过修改配置文件以使用外部SMTP服务器,达到不使用sendmail而用外部的smtp服务器发送邮件的目的, 操作如下: 一.安装mailx与sendmail # yum -y inst ...

  6. rm -rf python 实现 v0.1

    #coding=utf- import os def join(arr,join_falg): res = "" for a in arr: res += a+join_falg ...

  7. Codeforces Round #313 (Div. 2) C. Gerald's Hexagon(补大三角形)

    C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. CentOS7单独安装Apache Bench压力测试工具

    用于并发测试. 1.安装 # need to run ab,apache portable runtime yum install apr-util # yum install yum-utils # ...

  9. 在Ubuntu主机下实现与Windows虚拟机共享文件夹

    一.概述 由于要实现Ubuntu主机中的一些文件与Windows虚拟机共享,因此要创建一个共享文件夹映射到虚拟机中. 网上许多都是Windows主机+Linux虚拟机的配置,在此分享主机是Linux的 ...

  10. 框架源码系列二:手写Spring-IOC和Spring-DI(IOC分析、IOC设计实现、DI分析、DI实现)

    一.IOC分析 1. IOC是什么? IOC:Inversion of Control控制反转,也称依赖倒置(反转) 问题:如何理解控制反转? 反转:依赖对象的获得被反转了.由自己创建,反转为从IOC ...