File size: 3,287 Bytes
51b0588 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import os
import json
import sys
EXTRA_FILE = "ponyDiffusionV6XL_LoRAReadme.txt" # Readmeのファイル名
class Section:
def __init__(self, title, trigger, content):
self.title = title
self.trigger = trigger
self.content = content
def write_to_file(self):
filename = f"{self.title}.txt"
if filename == "このファイルについて.txt":
pass
elif not os.path.exists(filename):
with open(filename, 'w', encoding='utf-8-sig') as file: # UTF-8 BOM付きで書き込む
file.write(self.content)
print(f"{section.title}.txtと{section.title}.jsonを出力しました")
else:
pass
# print(f"File {filename} は既に出力済みです")
def write_to_json(self):
filename = f"{self.title}.json"
if filename == "このファイルについて.json":
pass
elif not os.path.exists(filename):
data = {
"description": self.content,
"sd version": "SDXL",
"activation text": self.trigger,
"preferred weight": 0,
"negative text": "",
"notes": ""
}
with open(filename, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
else:
pass
# print(f"File {filename} は既に出力済みです")
def extract_sections(filename):
sections = {}
with open(filename, 'r', encoding='utf-8') as file:
lines = file.readlines()
current_title = ""
current_trigger = ""
current_content = []
inside_section = False
for line in lines:
stripped_line = line.strip()
if stripped_line.startswith('■'):
if inside_section:
sections[current_title] = Section(current_title, current_trigger, "\n".join(current_content))
current_title = stripped_line[1:].strip()
current_trigger = ""
current_content = []
inside_section = True
elif "で呼び出し" in stripped_line:
current_trigger = stripped_line.replace("で呼び出し", "").strip()
current_content.append(stripped_line)
elif stripped_line == "":
if inside_section:
sections[current_title] = Section(current_title, current_trigger, "\n".join(current_content))
current_title = ""
current_trigger = ""
current_content = []
inside_section = False
else:
current_content.append(stripped_line)
if inside_section:
sections[current_title] = Section(current_title, current_trigger, "\n".join(current_content))
return sections
if __name__ == "__main__":
#READMEを読み込み、txtとjsonを作成
try:
sections = extract_sections(EXTRA_FILE)
except:
print(f"{EXTRA_FILE}が存在しません。")
sys.exit()
for title, section in sections.items():
section.write_to_file()
section.write_to_json()
|