ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: reformatting clipboard text on win32
Submitter: June Kim (other recipes)
Last Updated: 2002/09/29
Version no: 1.2
Category: Text

 

Not Rated yet


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.



Add comment

No comments.



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Changing return value ...

5. Quantum Superposition

6. Pickle objects under ...

7. Generalized delegates ...

8. Reorder a sequence (uses ...

9. Setting Win32 System ...

10. ObjectMerger




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.