|
Description:
Reformat the clipboard text.
Source: Text Source
"""
+ This code reformats:
abc def
ghi kjl
ioe.
hoa aho
ulm dij.
+ into:
abc def ghi kjl ioe.
hoa aho ulm dij.
"""
import win32clipboard as w
import win32con,re
def getText():
w.OpenClipboard()
d=w.GetClipboardData(win32con.CF_TEXT)
w.CloseClipboard()
return d
def setText(aType,aString):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(aType,aString)
w.CloseClipboard()
def changeClipboardBy(aFunction):
result=aFunction(getText().replace('\r\n','\n'))
setText(win32con.CF_TEXT,result.replace('\n','\r\n'))
def paragraph(aString):
aString=re.sub(r'(?m)^\s*$','',aString)
aString=re.sub(r'(?<!\n)\n(?!\n|$)',' ',aString)
return aString
if __name__=='__main__':
changeClipboardBy(paragraph)
Discussion:
Sometimes you need to reformat a piece of text, which is
newlined on every n-th column like e-mail or usenet post
texts, so that each paragraph has one single newline
characters.
Use this code on win32. Make a shortcut icon on your
desktop and assign a hotkey(from the properties window).
Just select any area of text which you want to reformat
and then press the hotkey. You can now paste the reformatted
text anywhere you want to.
This module is reusable where you want to replace some
piece of text on the fly. For example, you can use this module
along with Reindenter in Tools/Scripts/reindent.py to reformat
a piece of python code with clipboard ; you might need to fake
file objects with StringIO in this case, since the class accepts
file objs and writes on file objs, too.
|