其他分享
首页 > 其他分享> > pyhon对excel的xls与xlsx的读取,写入

pyhon对excel的xls与xlsx的读取,写入

作者:互联网

import shutil
import os

from openpyxl import load_workbook
from xlutils.copy import copy
import win32com.client as win32
import xlrd


def copyfiles3p3k():
rootdir="F:\\数据备份20211108\\材料模版\\back\\1标准钢构\\3p3k\\"
sourcefile=rootdir+"\\aa.xls"
# listdir=os.listdir(rootdir)
for fileindex in range(24,41,2):
filename="3p3k"+str(fileindex)+"k.xls"
# print(filename)
file_full_name=rootdir+filename
print(file_full_name)
shutil.copyfile(sourcefile,file_full_name)

def copyfiles3p4k():
rootdir="F:\\数据备份20211108\\材料模版\\back\\1标准钢构\\3p4k\\"
sourcefile=rootdir+"\\aa.xls"
# listdir=os.listdir(rootdir)
for fileindex in range(24,41,2):
filename="3p4k"+str(fileindex)+"k.xls"
# print(filename)
file_full_name=rootdir+filename
print(file_full_name)
shutil.copyfile(sourcefile,file_full_name)

def copyfiles6p3k():
rootdir="F:\\数据备份20211108\\材料模版\\back\\1标准钢构\\6p3k\\"
sourcefile=rootdir+"\\aa.xls"
# listdir=os.listdir(rootdir)
for fileindex in range(24,41,2):
filename="6p3k"+str(fileindex)+"k双楼梯.xls"
# print(filename)
file_full_name=rootdir+filename
print(file_full_name)
shutil.copyfile(sourcefile,file_full_name)

def copyfiles6p4k():
rootdir="F:\\数据备份20211108\\材料模版\\back\\1标准钢构\\6p4k\\"
sourcefile=rootdir+"\\aa.xls"
# listdir=os.listdir(rootdir)
for fileindex in range(24,41,2):
filename="6p4k"+str(fileindex)+"k双楼梯.xls"
# print(filename)
file_full_name=rootdir+filename
print(file_full_name)
shutil.copyfile(sourcefile,file_full_name)

def read_and_modify_cell(rootdir,prefilename,sheetname,lastname,worksheet_read,rowcount):
# cell(行,列)所有单元格从0开始
wfn = int(worksheet_read.cell(6,4).value+1)
# print(wfn)#这个是得到板房K数
# 要写入的文件,找到表
path=rootdir+prefilename+str(wfn)+lastname
print(path)
rb = xlrd.open_workbook(path)
wb = copy(rb)
ws = wb.get_sheet(0)
for i in range(2, rowcount):
for j in range(0, 5, 2):
# print(str(worksheet_read.cell(i, j).value))
ws.write(i, j, label=worksheet_read.cell(i, j).value)
wb.save(path)
# exit()

def modify_bbxls():
readfilename = 'F:\\数据备份20211108\\材料模版\\bb - 副本.xlsx'
savefilename='F:\\数据备份20211108\\材料模版\\bb - 副本.xls'
# 打开文件
workbook_read_write=load_workbook(readfilename)
# 得到要修改的表
worksheet_read_write=workbook_read_write.get_sheet_by_name('计算')
# 修改单元格
for fileindex in range(24, 41, 2):
# print(fileindex)
worksheet_read_write['b3']=str(fileindex)
# 写入模板单元格
workbook_read_write.save(readfilename)
# 把要读取的xlsx改成xls
if(os.path.isfile(savefilename)):
os.remove(savefilename)
excel=win32.gencache.EnsureDispatch('excel.application')
pro=excel.Workbooks.Open(readfilename)
pro.SaveAs(savefilename,FileFormat=56)
pro.Close(True)
excel.Application.Quit()
# 得到板房模板
sheetname=''
sheettype='6p4k'
rootdir="F:\\数据备份20211108\\材料模版\\back\\1标准钢构\\6p4k\\"
lastname='k双楼梯.xls'
rowcount=0
if sheettype=='3p3k':
rowcount=27
sheetname='单层3k材料';
elif sheettype=='3p4k':
rowcount=28
sheetname = '单层4k材料';
elif sheettype=='6p3k':
rowcount=43
sheetname = '双层3k材料';
elif sheettype=='6p4k':
rowcount=44
sheetname = '双层4k材料';

worksheet_read=xlrd.open_workbook(savefilename).sheet_by_name(sheetname)
read_and_modify_cell(rootdir,sheettype,sheetname,lastname,worksheet_read,rowcount)


if __name__ == '__main__':
print('PyCharm')
modify_bbxls()
# copyfiles3p4k()
# copyfiles6p3k()
# copyfiles6p4k()
# copyfiles3p3k()

标签:xlsx,name,read,pyhon,excel,filename,rootdir,print,xls
来源: https://www.cnblogs.com/gangliao81/p/16113734.html