Neculai Fântânaru

Everything Depends On The Leader

Python: Copy The Contents Of Text Files In The Body Of Html Files

On December 23, 2021
, in
Python Scripts Examples by Neculai Fantanaru

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

Alatura-te Comunitatii Neculai Fantanaru
The 63 Greatest Qualities of a Leader
Cele 63 de calităţi ale liderului

Why read this book? Because it is critical to optimizing your performance. Because it reveals the main coordinates after that are build the character and skills of the leaders, highlighting what it is important for them to increase their influence.

Leadership - Magic of Mastery
Atingerea maestrului

The essential characteristic of this book in comparison with others on the market in the same domain is that it describes through examples the ideal competences of a leader. I never claimed that it's easy to become a good leader, but if people will...

The Master Touch
Leadership - Magia măiestriei

For some leaders, "leading" resembles more to a chess game, a game of cleverness and perspicacity; for others it means a game of chance, a game they think they can win every time risking and betting everything on a single card.

Leadership Puzzle
Leadership Puzzle

I wrote this book that conjoins in a simple way personal development with leadership, just like a puzzle, where you have to match all the given pieces in order to recompose the general image.

Performance in Leading
Leadership - Pe înţelesul tuturor

The aim of this book is to offer you information through concrete examples and to show you how to obtain the capacity to make others see things from the same angle as you.

Leadership for Dummies
Leadership - Pe înţelesul tuturor

Without considering it a concord, the book is representing the try of an ordinary man - the author - who through simple words, facts and usual examples instills to the ordinary man courage and optimism in his own quest to be his own master and who knows... maybe even a leader.