import xlrd
import xlwt
from xlwt import Workbook
import os
import os.path
from openpyxl.reader.excel import load_workbook
from xlutils.copy import copy
# 遍历目录里所有的excel文件,并修改每一个文件特定的单元格
def modifycell():
rootdir = 'C:\\Users\\Administrator\\Documents\\七色米模版\\标准钢构\\3p3k'
list = os.listdir(rootdir)
for i in range(0, len(list)):
path = os.path.join(rootdir, list[i])
print(path)
rb = xlrd.open_workbook(path)
wb = copy(rb)
ws = wb.get_sheet(0)
# for i in range(3, 28):
# for j in range(0, 3,2):
#
# print(i, j )
# ws.write(i, j, label='')
# wb.save(path)
def read_and_modify_sapcialcell():
# 得到需要读取的文件中的表格
readfilename = 'F:\\数据备份20211108\\材料模版\\1标准钢构\\3p4k\\aa.xls'
workbook1 = xlrd.open_workbook(readfilename)
sheet1 = workbook1.sheet_by_name('Sheet1')
# 遍历整个文件夹
rootdir = 'F:\\数据备份20211108\\材料模版\\3全雅致钢构\\6p4k'
list = os.listdir(rootdir)
for a in range(0, len(list)):
if(list[a]=='aa.xls'):
continue
path = os.path.join(rootdir, list[a])
print(path)
strpath=path
rbook = xlrd.open_workbook(path)
wb = copy(rbook)
ws = wb.get_sheet(0)
for i in range(2, 25):
for j in range(0, 3, 2):
# print(str(sheet1.cell(i, j).value))
ws.write(i, j, label=sheet1.cell(i, j).value)
wb.save(path)
# 批量更改目录下所有文件表格数据
def read_write_all_biaozhunganggou_3p3k_excel():
rootdir = 'F:\\数据备份20211108\\材料模版\\back\\1标准钢构\\3p3k'
# 得到需要读取的文件中的表格
readfilename = rootdir+'\\aa.xls'
workbook1 = xlrd.open_workbook(readfilename)
sheet1 = workbook1.sheet_by_name('Sheet1')
# 得到整个文件夹里的文件名
listdir = os.listdir(rootdir)
# 遍历所有文件,根据需要修改每一个文件
for fileindex in range(0,len(listdir)):
# 如果文件名是aa.xls就跳过
if (listdir[fileindex] == 'aa.xls'):
continue
# 得到完整路径
path = os.path.join(rootdir, listdir[fileindex])
print(path)
# 通过路径得到需要写入的excel表格
rbook = xlrd.open_workbook(path)
wb = copy(rbook)
ws = wb.get_sheet(0)
# 循环读取要读取的文件里的表格的数据然后写进要写进的相应单元格里
for i in range(2, 27):
for j in range(0, 3, 2):
print(str(sheet1.cell(i, j).value))
ws.write(i, j, label=sheet1.cell(i, j).value)
# 下面代码用来删除特定单元格内容
# for erasecell in range(0,5,2):
# # print(sheet1.cell(27, erasecell).value)
# ws.write(27,erasecell, label='')
# wb.save(path)
def read_write_all_biaozhunganggou_3p4k_excel():
rootdir = 'F:\\数据备份20211108\\材料模版\\back\\1标准钢构\\3p4k'
# 得到需要读取的文件中的表格
readfilename = rootdir+'\\aa.xls'
workbook1 = xlrd.open_workbook(readfilename)
sheet1 = workbook1.sheet_by_name('Sheet1')
# 得到整个文件夹里的文件名
listdir = os.listdir(rootdir)
# 遍历所有文件,根据需要修改每一个文件
for fileindex in range(0,len(listdir)):
# 如果文件名是aa.xls就跳过
if (listdir[fileindex] == 'aa.xls'):
continue
# 得到完整路径
path = os.path.join(rootdir, listdir[fileindex])
print(path)
# 通过路径得到需要写入的excel表格
rbook = xlrd.open_workbook(path)
wb = copy(rbook)
ws = wb.get_sheet(0)
# 循环读取要读取的文件里的表格的数据然后写进要写进的相应单元格里
for i in range(2, 28):
# 下面代码用来删除特定单元格内容。
# 删除要慎用,用之前一定记得备份
for erasecell in range(0,5,2):
# print(sheet1.cell(28, erasecell).value)
ws.write(28,erasecell, label='')
# 下面代码用来写入特定单元格相应内容
for j in range(0, 3, 2):
# print(str(sheet1.cell(i, j).value))
ws.write(i, j, label=sheet1.cell(i, j).value)
# wb.save(path)
def read_write_all_biaozhunganggou_6p3k_excel():
rootdir = 'F:\\数据备份20211108\\材料模版\\back\\2半雅致钢构\\6p3k'
# 得到需要读取的文件中的表格
readfilename = rootdir+'\\aa.xls'
workbook1 = xlrd.open_workbook(readfilename)
sheet1 = workbook1.sheet_by_name('Sheet1')
# 得到整个文件夹里的文件名
listdir = os.listdir(rootdir)
# 遍历所有文件,根据需要修改每一个文件
for fileindex in range(0,len(listdir)):
# 如果文件名是aa.xls就跳过
if (listdir[fileindex] == 'aa.xls'):
continue
# 得到完整路径
path = os.path.join(rootdir, listdir[fileindex])
print(path)
# 通过路径得到需要写入的excel表格
rbook = xlrd.open_workbook(path)
wb = copy(rbook)
ws = wb.get_sheet(0)
# 循环读取要读取的文件里的表格的数据然后写进要写进的相应单元格里
for i in range(2, 43):
# 下面代码用来删除特定单元格内容。
# 删除要慎用,用之前一定记得备份
# for erasecell in range(0,5,2):
# print(sheet1.cell(28, erasecell).value)
# ws.write(28,erasecell, label='')
# 下面代码用来写入特定单元格相应内容
listcell=[]
for j in range(0, 3, 2):
listcell.append(sheet1.cell(i, j).value)
# ws.write(i, j, label=sheet1.cell(i, j).value)
print(listcell)
# wb.save(path)
def read_write_all_biaozhunganggou_6p4k_excel():
rootdir = 'F:\\数据备份20211108\\材料模版\\back\\2半雅致钢构\\6p4k'
# 得到需要读取的文件中的表格
readfilename = rootdir+'\\aa.xls'
workbook1 = xlrd.open_workbook(readfilename)
sheet1 = workbook1.sheet_by_name('Sheet1')
# 得到整个文件夹里的文件名
listdir = os.listdir(rootdir)
# 遍历所有文件,根据需要修改每一个文件
for fileindex in range(0,len(listdir)):
# 如果文件名是aa.xls就跳过
if (listdir[fileindex] == 'aa.xls'):
continue
# 得到完整路径
path = os.path.join(rootdir, listdir[fileindex])
print(path)
# 通过路径得到需要写入的excel表格
rbook = xlrd.open_workbook(path)
wb = copy(rbook)
ws = wb.get_sheet(0)
# 循环读取要读取的文件里的表格的数据然后写进要写进的相应单元格里
for i in range(2, 44):
# 下面代码用来删除特定单元格内容。
# 删除要慎用,用之前一定记得备份
# for erasecell in range(0,5,2):
# print(sheet1.cell(28, erasecell).value)
# ws.write(28,erasecell, label='')
# 下面代码用来写入特定单元格相应内容
listcell=[]
for j in range(0, 3, 2):
listcell.append(sheet1.cell(i, j).value)
# ws.write(i, j, label=sheet1.cell(i, j).value)
print(listcell)
# wb.save(path)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print('PyCharm')
# read_write_all_biaozhunganggou_3p4k_excel()
# read_write_all_biaozhunganggou_6p3k_excel()
read_write_all_biaozhunganggou_6p4k_excel()
# 修改xls文件里的单元格
# rb = xlrd.open_workbook('c:\\1.xls') # 打开weng.xls文件
# wb = copy(rb) # 利用xlutils.copy下的copy函数复制
# ws = wb.get_sheet(0) # 获取表单0
# ws.write(30, 0, label='好的') # 增加(8,0)的值
# wb.save('c:\\1.xls')
# 读取excel文件里的单元格
# workbook1=xlrd.open_workbook("c:\\1.xls")
# sheet1=workbook1.sheet_by_name("Sheet1")
# print(sheet1.cell(0,1).value)
本文暂时没有评论,来添加一个吧(●'◡'●)