LoginSignup
0
0

More than 3 years have passed since last update.

改行をすべて消すだけのコード

Posted at

動機

PDFからコピペして翻訳しようとすると、改行が邪魔。

できたもの

貼り付けた瞬間に、以下の状態で改めてクリップボードにコピーします。
- 改行を半角スペースに変えた状態(ヨーロッパ言語への対応)
- 半角スペースが2つ続いている場合は、1つだけにした状態

image.png

PyInstallerとかで使える状態にしてください。

import sys

import pyperclip
from PyQt5.QtWidgets import QApplication, QMainWindow, QPlainTextEdit

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        width = 250
        height = 100
        self.setFixedSize(width, height)
        self.setWindowTitle('\\n deleter')

        self.textBox = QPlainTextEdit(self)
        self.textBox.textChanged.connect(self.copy_sentence)
        self.textBox.resize(width, height)
        self.textBox.setPlaceholderText ('Paste your sentence here.')

        self.show()

    def copy_sentence(self):
        sentence = self.textBox.toPlainText()
        no_n_sentence = sentence.replace('\n',' ')
        no_double_space = no_n_sentence.replace('  ', ' ')
        pyperclip.copy(no_double_space)
        self.statusBar().showMessage('Copied!', msecs=3000)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())



0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0