表格内容
import os
import pandas as pd
class DataHandle:
def __init__(self):
self.currentPath = os.path.dirname(__file__)
self.excelPath = self.currentPath + "/reFile.xlsx"
self.reExcelFile = pd.read_excel(self.excelPath, sheet_name=None, engine='openpyxl')
def writePyFile(self):
tempName = ''
number = []
for sheetName, sheetData in self.reExcelFile.items():
for rowIndex, row in sheetData.iterrows():
if len(number) > 0 and row['id'] not in number:
self.finalData(f"{self.currentPath}/temp.py", f"{self.currentPath}/{tempName}.py", tempName)
tempName = row['id']
if len(number) == 0 or tempName not in number:
self.preData(f"{self.currentPath}/temp.py")
writeContent = f"""
def step(self):
path1 = "{self.currentPath}/{row['preText']}"
path2 = "{self.currentPath}/{row['preText']}.jpeg"
print("{row['text']}")
"""
with open(f"{self.currentPath}/temp.py", 'a', encoding='UTF-8') as fileWriteContent:
fileWriteContent.write(writeContent)
number.append(row['id'])
self.finalData(f"{self.currentPath}/temp.py", f"{self.currentPath}/{tempName}.py", tempName)
def preData(self, filePath):
headContent = f"""
import os
import pandas as pd
class Temp:
def __init__(self):
self.type = 12
"""
with open(filePath, 'w', encoding='UTF-8') as fileWriteHead:
fileWriteHead.write(headContent)
def finalData(self, filePath, reFilePath, replaceName):
finalContent = f"""
if __name__ == "__main__":
test()
"""
with open(filePath, 'a', encoding='UTF-8') as fileWriteFinal:
fileWriteFinal.write(finalContent)
with open(filePath, 'r', encoding='UTF-8') as readFile:
content = readFile.read()
content = content.replace('Temp', replaceName)
with open(filePath, 'w', encoding='UTF-8') as writeFile:
writeFile.write(content)
os.rename(filePath, reFilePath)
DataHandle().writePyFile()
最终效果: