Advertisement
MizunoBrasil

Gerador de Noticias/Textos em html

Jun 27th, 2023
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. import sys
  2. from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QTextEdit, QPushButton
  3. from PyQt5.QtGui import QFont
  4. from PyQt5.QtCore import Qt
  5.  
  6. class MainWindow(QMainWindow):
  7.     def __init__(self):
  8.         super().__init__()
  9.         self.setWindowTitle("Gerador de texto em HTML")
  10.         self.setGeometry(100, 100, 640, 800)  # Define a posição e o tamanho da janela
  11.  
  12.         # Criação dos widgets
  13.         label_titulo = QLabel("Título")
  14.         label_titulo.setFont(QFont("Arial", 16, QFont.Bold))  # Define a fonte e o tamanho da fonte da label "Título"
  15.         self.caixa_titulo = QTextEdit()
  16.         self.caixa_titulo.setFont(QFont("Arial", 14))  # Define a fonte e o tamanho da fonte da caixa de texto do título
  17.         self.caixa_titulo.setFixedHeight(40)  # Define a altura desejada para a área de texto do título
  18.         self.caixa_titulo.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)  # Remove a barra de rolagem vertical
  19.         label_texto = QLabel("Texto")
  20.         label_texto.setFont(QFont("Arial", 16, QFont.Bold))  # Define a fonte e o tamanho da fonte da label "Texto"
  21.         self.caixa_texto = QTextEdit()
  22.         self.caixa_texto.setFont(QFont("Arial", 14))  # Define a fonte e o tamanho da fonte da caixa de texto do texto
  23.         botao_gerar = QPushButton("Gerar")
  24.         botao_gerar.clicked.connect(self.gerar_registro)
  25.         botao_gerar.setFixedHeight(50)  # Define a altura desejada para o botão "Gerar"
  26.         botao_gerar.setFont(QFont("Arial", 18))  # Define a fonte e o tamanho da fonte do botão "Gerar"
  27.  
  28.         # Layout
  29.         layout = QVBoxLayout()
  30.         layout.addWidget(label_titulo)
  31.         layout.addWidget(self.caixa_titulo)
  32.         layout.addWidget(label_texto)
  33.         layout.addWidget(self.caixa_texto)
  34.         layout.addWidget(botao_gerar)
  35.  
  36.         # Widget principal
  37.         widget = QWidget()
  38.         widget.setLayout(layout)
  39.         self.setCentralWidget(widget)
  40.  
  41.         # Centraliza a janela na tela
  42.         screen_geometry = QApplication.desktop().screenGeometry()
  43.         x = (screen_geometry.width() - self.width()) // 2
  44.         y = (screen_geometry.height() - self.height()) // 2
  45.         self.move(x, y)
  46.  
  47.     def gerar_registro(self):
  48.         titulo = self.caixa_titulo.toPlainText()
  49.         texto = self.caixa_texto.toPlainText()
  50.  
  51.         # Substitui quebras de linha por tags <p> e </p>
  52.         texto_formatado = "<p>" + texto.replace("\n", "</p><p>") + "</p>"
  53.  
  54.         # Registro HTML
  55.         registro_html = f'''
  56.        <!DOCTYPE html>
  57.        <html lang="pt-br">
  58.        <head>
  59.            <meta charset="UTF-8">
  60.            <meta http-equiv="X-UA-Compatible" content="IE=edge">
  61.            <meta name="viewport" content="width=device-width, initial-scale=1.0">
  62.            <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  63.            <title>Documento</title>
  64.        </head>
  65.        <body>
  66.            <div class="container">
  67.                <h1>{titulo}</h1>
  68.                {texto_formatado}
  69.                <hr>
  70.            </div>
  71.            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  72.        </body>
  73.        </html>
  74.        '''
  75.  
  76.         # Adiciona o registro ao arquivo HTML
  77.         with open('output.html', 'a', encoding='utf-8') as arquivo:
  78.             arquivo.write(registro_html)
  79.  
  80.         # Exemplo: exibir o registro HTML no console
  81.         print(registro_html)
  82.  
  83. if __name__ == '__main__':
  84.     app = QApplication(sys.argv)
  85.     window = MainWindow()
  86.     window.show()
  87.     sys.exit(app.exec_())
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement