You can view the full code here: https://pastebin.com/vA56f8pN
Install Python.
Let's say we have several .txt files. And we only have one .html file
In the body of the .html file there is this section: < ! -- ARTICOL START --> and < ! -- ARTICOL FINAL -->. The text in the .txt file will be copied here.
You will also need to create a folder named: fisiere_html (all new files will be saved here). All other files. txt and the .html file are free outside the folder: fisiere_html
You can have as many .txt files as you want. Such a .txt file will contain only text, such as:
My name probably does not say anything to you, Walter Elias Disney,
because I just came back from New York and got only 40 dollars in my pocket.
Increasingly, I need spiritual points to help me not get swallowed by the
most painful miscarriage, the saddest abandon in a labyrinth of vanity
where the only marks are self-forgetting and the others, followed in the
end of supreme sacrifice.
You will only need one .html file containing the following section:
<!-- ARTICOL START --> <p class="obisnuit"><em>Honor your moral and spiritual obligations.</em></p> <p class="nint">Bishop knew how to say the most meaningful of things speech.</p> <!-- ARTICOL FINAL -->
OUTPUT: after running the code, we will have:
<!-- ARTICOL START -->
My name probably does not say anything to you, Walter Elias Disney,
because I just came back from New York and got only 40 dollars in my pocket.
Increasingly, I need spiritual points to help me not get swallowed by the
most painful miscarriage, the saddest abandon in a labyrinth of vanity
where the only marks are self-forgetting and the others, followed in the
end of supreme sacrifice.
<!-- ARTICOL FINAL -->
CODUL: Copy and run the code below in any interpreter program (I am using pyScripter) . Don't forget to change the path on the line "creare_fisiere_html".
import os import re def read_text_from_file(file_path): """ Aceasta functie returneaza continutul unui fisier. file_path: calea catre fisierul din care vrei sa citesti """ with open(file_path, encoding='utf8') as f: text = f.read() return text def write_to_file(text, file_path): """ Aceasta functie scrie un text intr-un fisier. text: textul pe care vrei sa il scrii file_path: calea catre fisierul in care vrei sa scrii """ with open(file_path, 'wb') as f: f.write(text.encode('utf8', 'ignore')) def copiaza_continut_txt_html(cale_fisier_txt, cale_fisier_html): # astea sunt argumentele functiei, adica cand apelez functia # citesti textul din fisier text_txt = read_text_from_file(cale_fisier_txt) text_html = read_text_from_file(cale_fisier_html) # transformam textul din fisier intr-un string text_txt = str(text_txt) text_html = str(text_html) # aici e pattern-ul pentru expresia regex; (.*?) inseamna ca preia tot ce este intre tag-uri # modifici expresia regulata in functie de ce tag dai ca argument pentru functie articol_pattern = re.compile('<!-- ARTICOL START -->([\s\S]*?)<!-- ARTICOL FINAL -->[\s\S]*?') text_articol = re.findall(articol_pattern, text_html) if len(text_articol) != 0: text_articol = str(text_articol[0]) text_txt = '\n\n' + text_txt + '\n\n' text_html = text_html.replace(text_articol, text_txt) file_path = os.path.dirname(cale_fisier_txt) + "\\" + "fisiere_html" + "\\" + os.path.splitext(os.path.basename(cale_fisier_txt))[0] + '.html' write_to_file(text_html, file_path) print("Scriere efectuata cu succes.") else: print("Fisier html fara ARTICOL START/FINAL.") def creare_fisiere_html(cale_folder_txt, cale_fisier_html): """ Functia itereaza printr-un folder care contine fisiere txt si creeaza fisiere html corespunzatoare """ for f in os.listdir(cale_folder_txt): if f.endswith('txt'): cale_fisier_txt = cale_folder_txt + "\\" + f copiaza_continut_txt_html(cale_fisier_txt, cale_fisier_html) else: continue def main(): creare_fisiere_html("c:\\Folder1", "c:\\Folder1\\oana.html") if __name__ == '__main__': main()
That's all folks.
If you like my code, then make me a favor: translate your website into Romanian, "ro".
Also, see this VERSION 2 or VERSION 3 or VERSION 4 or VERSION 5 or VERSION 6 or VERSION 7