python批量根据excel表行列信息更改txt文本多行多项内容#

#求助#python批量根据excel表行列信息更改txt文本文件#
已有条件为:位于(D:\测试\测试原始副本)目录下有名为 输入命令的txt文本(输入命令.txt)
以及位于D:目录下的"参数序列.xlsx"的表格,主要内容如下所示:

img

需要完成以下操作,批量复制“测试原始副本”文件夹到目录(D:\新测试)下,并以N+excel表中第一列“编号”的数字重新命名,具体如N1,N2,...,N20,N21
然后修改复制过来对应文件夹中的(输入命令.txt)文件,这里希望可以封装为一个函数(只是修改txt的部分)。
修改的内容为:
将11行第1项修改为“T1A”列的数值×1000(以科学计数法表示),第2项内容修改为“T1B”列的数值
将14行第1项修改为“T2A”列的数值×1000(以科学计数法表示),第2项内容修改为“T2B”列的数值
将17行第1项修改为“T3A”列的数值×1000(以科学计数法表示),第2项内容修改为“T3B”列的数值
将21行第1项修改为“T4A”列的数值×1000(以科学计数法表示),第2项内容修改为“T4B”列的数值
划重点,!!替换的是第几第几项,而非这一整行,当前这四行均为8项(但txt文本里并不都是),替换位置如图所示

img

其中用于修改的数值必须与excel最前面的编号对应,如N1文件夹下的txt修改,N1里用来替换的值为:T1A(15),T1B(16),..,T4A(10),T4B(15),即N1右边的这一行数字,N2文件夹里txt文本修改时则用N2右边的这一行数字t,xt文本里对应黄色背景位置的值被括号里的数值替换,其他的以此用表格类推(excel一行一行往下)。

“测试原始副本”文件夹,目录下有一大堆txt文件?这里没有写清楚,其他的看起来实现并不困难,如果有没有说清楚的可以详聊

这个人问题看着很眼熟啊, 我记得问你要过excel 文件,我要本地跑来着, 你没给, 这次可以给一下吗


# -*- coding: GBK -*-
 
from xlutils.copy import copy
import xlrd
import random
 
 
# 缓存原excel内容
data = xlrd.open_workbook(filename=r'./1.xlsx')
# 索引下标为1的sheet页
 
new_excel = copy(data)
 
sheet11 = data.sheet_by_index(0) #用于获取sheet1的最大行数和列数以及单元格数据
sheet1 = new_excel.get_sheet(0) #通过copy,sheet1;方便修改和保存
 
cols = sheet11.ncols #有效的最大列数
rows = sheet11.nrows #有效的最大行数
#print(cols)
#print(rows)
#print(type(sheet11.cell_value(0,0))) #查看复制过来的A11的数据类型
#发现数据类型,纯数字为float ; 文字、空格、字母都是 str型;
for r in range(0,rows): #此处的单元格从(0,0)开始;区别于openpyxl
    for c in range(0,cols):
        content = sheet11.cell_value(r,c)  #获取该行该列单元格的内容
        
        if(isinstance(content, float)): #判断是否为float
               #其实直接写isinstance(content, float)就行;
                #但是我写的时候,就想多此一举而已;
            T = True
        else:
            T = False
 
        if(content !=None ):  # 如果该单元格内容不为空
            if(T):#if(isinstance(content, float))
                content = int(float(content)) #将float转化成int
        
                if content>0 and content<=5:
                    sheet1.write(r,c,random.randint(1,5)) #采用copy的sheet1;通过wirte直接修改
                if content>5 and content<=10:
                    sheet1.write(r,c,random.randint(5,10))
                if content>10 and content<=20:
                    sheet1.write(r,c,random.randint(10,20))
                if content>20 and content<=50:
                    ZF = random.choice((-1, 1)) #随机选择 +1或者-1
                    ran_num = random.randint(1,7) #随机出现 [1,7]的正整数
                    num = content + (ZF*ran_num)
                    print(str(content)+'....'+str(num))
                    sheet1.write(r,c,num)
                if content>50 and content<=200:
                    ZF = random.choice((-1, 1))
                    ran_num = random.randint(1,15)
                    num = content + (ZF*ran_num)
                    print(str(content)+'....'+str(num))
                    sheet1.write(r,c,num)
                if content>200 and content<=500:
                    ZF = random.choice((-1, 1))
                    ran_num = random.randint(1,35)
                    num = content + (ZF*ran_num)
                    print(str(content)+'....'+str(num))
                    sheet1.write(r,c,num)
                if content>500 and content<=1000:
                    ZF = random.choice((-1, 1))
                    ran_num = random.randint(1,55)
                    num = content + (ZF*ran_num)
                    print(str(content)+'....'+str(num))
                    sheet1.write(r,c,num)
                if content>1000 and content<=10000:
                    ZF = random.choice((-1, 1))
                    ran_num = random.randint(1,115)
                    num = content + (ZF*ran_num)
                    print(str(content)+'....'+str(num))
                    sheet1.write(r,c,num)
                if content>10000 and content<=50000:
                    ZF = random.choice((-1, 1))
                    ran_num = random.randint(1,515)
                    num = content + (ZF*ran_num)
                    print(str(content)+'....'+str(num))
                    sheet1.write(r,c,num)
                if content>50000 :
                    ZF = random.choice((-1, 1))
                    ran_num = random.randint(1,2000)
                    num = content + (ZF*ran_num)
                    print(str(content)+'....'+str(num))
                    sheet1.write(r,c,num)
        
 
new_excel.save(r'./2.xlsx') #通过copy的excel直接保存;