import os,time,shutil,re,copy
import pandas as pd
import numpy as np
root_path=os.path.dirname(__file__)
output_path=os.path.join(root_path,'output',time.strftime('%Y%m%d'))
os.makedirs(output_path,exist_ok=True)
output_config_files_path=os.path.join(output_path,'config.files')
if os.path.exists(output_config_files_path):
shutil.rmtree(output_config_files_path)
shutil.copytree(
os.path.join(root_path,'config.files'),
output_config_files_path
)
with open(os.path.join(root_path,'config.files','sheet001.htm'),'r',encoding='utf8') as f:
html_lines=f.readlines()
blocks_dicts={}
pattern=r'<!-- block_(.*?) -->'
pattern_end=r'<!-- block_(.*?) end -->'
for i,line in enumerate(html_lines):
result=re.search(pattern,line)
if result:
if 'end' not in line:
blcok_name=result.group(1)
blocks_dicts[blcok_name]=[i,0]
else:
blocks_dicts[blcok_name][1]=i
print(blocks_dicts)
df=pd.read_excel(os.path.join(root_path,'config.xlsx'))
for row in range(df.shape[0]):
new_html_lines=copy.deepcopy(html_lines)
for line_i in range(*blocks_dicts['sp']):
new_html_lines[line_i]=html_lines[line_i].replace("{__{sp}__}",df.loc[row,'sp'])
trs_list=[]
for i,item in enumerate(df.loc[row,'项目1'].split()):
tr_list=copy.deepcopy( html_lines[blocks_dicts['tr'][0]+1:blocks_dicts['tr'][1]])
for sub_i,line in enumerate(tr_list):
if '项目1' in line:
tr_list[sub_i]=line.replace("{__{项目1}__}",df.loc[row,'项目1'].split()[i])
elif '项目2' in line:
tr_list[sub_i]=line.replace("{__{项目2}__}",df.loc[row,'项目2'].split()[i])
elif '项目3' in line:
tr_list[sub_i]=line.replace("{__{项目3}__}",df.loc[row,'项目3'].split()[i])
elif '项目4' in line:
tr_list[sub_i]=line.replace("{__{项目4}__}",df.loc[row,'项目4'].split()[i])
trs_list += tr_list
print(item)
new_html_lines=[
*new_html_lines[:blocks_dicts['tr'][0]+1],
*trs_list,
*new_html_lines[blocks_dicts['tr'][1]:],
]
with open(os.path.join(output_config_files_path,df.loc[row,'sp']+'.html'),'w',encoding='utf8') as f:
f.writelines(new_html_lines)