Popular Python recipes tagged "text"http://code.activestate.com/recipes/langs/python/tags/text/2017-04-07T12:30:13-07:00ActiveState Code RecipesA simple text file pager in Python (Python)
2017-02-10T21:34:45-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580755-a-simple-text-file-pager-in-python/
<p style="color: grey">
Python
recipe 580755
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/pagination/">pagination</a>, <a href="/recipes/tags/paging/">paging</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This recipe shows how to create a simple text file pager in Python. It allows you to view text content a page at a time (with a user-definable number of lines per page). Like standard Unix utilities, it can either take a text file name as a command-line argument, or can read the text from its standard input, which can be redirected to come from a file, or to come from a pipe. The recipe is for Windows only, though, since it uses the msvcrt.getch() function, which is Windows-specific. However, the recipe can be modified to work on Unix by using things like tty, curses, termios, cbreak, etc.</p>
<p>More details here:</p>
<p><a href="https://jugad2.blogspot.in/2017/02/tp-simple-text-pager-in-python.html" rel="nofollow">https://jugad2.blogspot.in/2017/02/tp-simple-text-pager-in-python.html</a></p>
Batch conversion of text files to PDF with fileinput and xtopdf (Python)
2016-11-07T20:28:01-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580715-batch-conversion-of-text-files-to-pdf-with-fileinp/
<p style="color: grey">
Python
recipe 580715
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/batchmode/">batchmode</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/text_processing/">text_processing</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>).
</p>
<p>This recipe shows how to do a batch conversion of the content of multiple text files into a single PDF file, with a) an automatic page break after the content of each text file (in the PDF output), b) page numbering, and c) a header and footer on each page.</p>
<p>It uses the fileinput module (part of the Python standard library), and xtopdf, a Python library for conversion of other formats to PDF.</p>
<p>xtopdf is available here: <a href="https://bitbucket.org/vasudevram/xtopdf" rel="nofollow">https://bitbucket.org/vasudevram/xtopdf</a></p>
<p>and a guide to installing and using xtopdf is here:</p>
<p><a href="http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html" rel="nofollow">http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html</a></p>
<p>Here is a sample run of the program:</p>
<p>python BTTP123.pdf text1.txt text2.txt text3.txt</p>
<p>This will read the content from the three text files specified and write it into the PDF file specified, neatly formatted.</p>
Tag entry for Tkinter like Jquery Tag It (Python)
2017-04-07T12:30:13-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580734-tag-entry-for-tkinter-like-jquery-tag-it/
<p style="color: grey">
Python
recipe 580734
by <a href="/recipes/users/4189907/">Miguel Martínez López</a>
(<a href="/recipes/tags/entry/">entry</a>, <a href="/recipes/tags/megawidget/">megawidget</a>, <a href="/recipes/tags/tag/">tag</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
Revision 2.
</p>
<p>Tag editing widget for tkinter. It's similar to jQuery tag it:
<a href="http://aehlke.github.io/tag-it/" rel="nofollow">http://aehlke.github.io/tag-it/</a></p>
Text widget width and height in pixels (Tkinter) (Python)
2016-12-11T13:39:08-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/578887-text-widget-width-and-height-in-pixels-tkinter/
<p style="color: grey">
Python
recipe 578887
by <a href="/recipes/users/4189907/">Miguel Martínez López</a>
(<a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
Revision 8.
</p>
<p>The solution consists in putting the Text widget inside a frame, forcing the frame to a fixed size by deactivating size propagation and configuring the Text widget to expand and fill both directions (to stick to the frame borders).</p>
<p><a href="https://stackoverflow.com/questions/14887610/how-to-specify-the-dimensions-of-a-tkinter-text-box-in-pixels" rel="nofollow">https://stackoverflow.com/questions/14887610/how-to-specify-the-dimensions-of-a-tkinter-text-box-in-pixels</a></p>
Automatic resized Text widget (Tkinter) (Python)
2016-12-10T15:40:25-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/578886-automatic-resized-text-widget-tkinter/
<p style="color: grey">
Python
recipe 578886
by <a href="/recipes/users/4189907/">Miguel Martínez López</a>
(<a href="/recipes/tags/autoresize/">autoresize</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
Revision 14.
</p>
<p>The Text widget resize to its contents.
Based on this:
<a href="https://stackoverflow.com/questions/11544187/tkinter-resize-text-to-contents" rel="nofollow">https://stackoverflow.com/questions/11544187/tkinter-resize-text-to-contents</a></p>
<p>But here there is one important improvement:
The widget resize first, before a character inclusion.</p>
<p>For this trick I have had to use tag bindings.</p>
<p>Here there is a practical application of this widget:</p>
<p><a href="https://code.activestate.com/recipes/578885-draggable-desktop-note-or-sticky-notes-in-tkinter/" rel="nofollow">https://code.activestate.com/recipes/578885-draggable-desktop-note-or-sticky-notes-in-tkinter/</a></p>
Call out to an external editor (Python)
2014-09-01T18:26:51-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578926-call-out-to-an-external-editor/
<p style="color: grey">
Python
recipe 578926
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/editing/">editing</a>, <a href="/recipes/tags/editor/">editor</a>, <a href="/recipes/tags/external/">external</a>, <a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>Here's a function that lets you use Python to wrap calls to an external editor. The editor can be an command line editor, like venerable old "ed", or something more powerful like nano, vim or emacs, and even GUI editors. After the editor quits, the text you typed in the editor is returned by the function.</p>
<p>A simple example, using the (rather cryptic) 'ed' editor on Linux. For the benefit of those unfamiliar with 'ed', I have annotated the editor session with comments.</p>
<pre class="prettyprint"><code>>>> status, text = edit('ed')
0 ## ed prints the initial number of lines
a ## start "append" mode
Hello World!
Goodbye now
. ## stop appending
w ## write the file to disk
25 ## ed prints the number of bytes written
q ## quit ed and return to Python
>>> status
0
>>> print text
Hello World!
Goodbye now
</code></pre>
Droids ( a text adventure ) (Python)
2013-07-27T09:44:06-07:00jayhttp://code.activestate.com/recipes/users/4186699/http://code.activestate.com/recipes/578618-droids-a-text-adventure/
<p style="color: grey">
Python
recipe 578618
by <a href="/recipes/users/4186699/">jay</a>
(<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/text/">text</a>).
</p>
<p>for beginners.</p>
Levenshtein, my love (Python)
2014-01-15T09:14:30-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578810-levenshtein-my-love/
<p style="color: grey">
Python
recipe 578810
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p><em>be kind and comment, especially if you downvote</em></p>
<p><strong>levenshtein_distance()</strong> is an implementation of the iterative algorithm for the levenshtein distance (cf. <a href="http://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_full_matrix%29" rel="nofollow">http://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_full_matrix)</a></p>
<p><strong>levenshtein_sequence()</strong> is an attempt to retrieve one of the levenshtein paths (the one that give priority to substitution, deletion, insertion, in this order). The result is a list of tuples made of:</p>
<ol>
<li>the operation ( <code>=</code>, <code>-</code>, <code>+</code>, <code>*</code> for respectively keep, delete, insert, substitute)</li>
<li>the coordinate in the first</li>
<li>and in the second string.</li>
</ol>
<pre class="prettyprint"><code>>>> levenshtein_sequence('saturday', 'sunday')
[('=', 0, 0), ('-', 1, 0), ('-', 2, 0), ('=', 3, 1), ('*', 4, 2), ('=', 5, 3), ('=', 6, 4), ('=', 7, 5)]
>>> levenshtein_sequence('kitten', 'sitting')
[('*', 0, 0), ('=', 1, 1), ('=', 2, 2), ('=', 3, 3), ('*', 4, 4), ('=', 5, 5), ('+', 5, 6)]
</code></pre>
<p>This code is part of foreplays, in a plan I have to improve difflib with alternative SequenceMatchers \o/</p>
<p><em>/!\ tab indented, as usual.</em></p>
Plain Text Editor in Python (Python)
2013-06-18T15:33:01-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578568-plain-text-editor-in-python/
<p style="color: grey">
Python
recipe 578568
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/editor/">editor</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/text_processing/">text_processing</a>).
</p>
<p>Just a simple text editor written in Python with Tk for graphics. </p>
<p>Check out my blog <a href="http://thelivingpearl.com/">Captain DeadBones Chronicles</a></p>
Droids 2 ( IF ) (Python)
2013-08-13T09:12:11-07:00jayhttp://code.activestate.com/recipes/users/4186699/http://code.activestate.com/recipes/578636-droids-2-if/
<p style="color: grey">
Python
recipe 578636
by <a href="/recipes/users/4186699/">jay</a>
(<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/if/">if</a>, <a href="/recipes/tags/learning/">learning</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<h4 id="-for-learners-and-beginners-like-myself-">-For learner's and beginner's like myself -</h4>
<h4 id="-and-for-anyone-who-may-have-enjoyed-droids-">-and for anyone who may have enjoyed Droids. -</h4>
Text Editor in Python 3.3 (Python)
2013-06-19T15:58:17-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578569-text-editor-in-python-33/
<p style="color: grey">
Python
recipe 578569
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/editor/">editor</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/text_processing/">text_processing</a>).
</p>
<p>This is a simple text editor written in Python using <code>tkinter</code> for graphics.</p>
<p>Check out Captain DeadBones' <a href="http://thelivingpearl.com/">Chronicles</a> blog.</p>
Easy to use, easy to read, python based HTML generation (Python)
2013-06-21T14:47:21-07:00Pavloshttp://code.activestate.com/recipes/users/4185038/http://code.activestate.com/recipes/578436-easy-to-use-easy-to-read-python-based-html-generat/
<p style="color: grey">
Python
recipe 578436
by <a href="/recipes/users/4185038/">Pavlos</a>
(<a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/template/">template</a>, <a href="/recipes/tags/text/">text</a>).
Revision 4.
</p>
<p>I was looking for a simple way to generate HTML directly in python that does not require learning a new template 'language' nor requires the installation of a big complex package. Closest thing I found was James Casbon's attempt(https://gist.github.com/1461441). This is my version of the same idea. </p>
<p>(2013-04-21) added some simplifications and support for switching off string interpolation. Added to github:</p>
<p><a href="https://github.com/pavlos-christoforou/web" rel="nofollow">https://github.com/pavlos-christoforou/web</a></p>
For Characters in a String, Replace with Character (Python)
2012-03-23T20:44:23-07:00Andrew Yurisichhttp://code.activestate.com/recipes/users/4180867/http://code.activestate.com/recipes/578086-for-characters-in-a-string-replace-with-character/
<p style="color: grey">
Python
recipe 578086
by <a href="/recipes/users/4180867/">Andrew Yurisich</a>
(<a href="/recipes/tags/character/">character</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/substitute/">substitute</a>, <a href="/recipes/tags/text/">text</a>).
Revision 9.
</p>
<p>Useful if you need to replace many different characters with all of the same character. Can accept an unlimited number of request to replace.</p>
<p>Does not work with words, only characters. You can, however, replace single characters with words. I may go back and re-implement it using tuples for the keys, but this would make searching the text for any matches pretty expensive, I'd imagine. At that point, it's mostly a job for a regex, and those tools already exist.</p>
Funny text generator (Python)
2011-12-25T11:14:39-08:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/577988-funny-text-generator/
<p style="color: grey">
Python
recipe 577988
by <a href="/recipes/users/1552957/">Pierre Quentel</a>
(<a href="/recipes/tags/generation/">generation</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/text/">text</a>).
Revision 4.
</p>
<p>This class takes a text (preferably long enough) and generates another text that "looks like" the original. It won't mean anything, or just by chance ;-)</p>
<p>For example, taking Hamlet, Act I, the program generates things like :</p>
<p>Hamlet</p>
<blockquote>
<p>And vanish'd from our watch; <br />
His further. Fare third nights of the flushing immortal as it draw you into the flushing thy complete steel <br />
'Tis sweet and each new-hatch'd: <br />
A country's father; <br />
To business and is prodigal thee! <br />
Have of crowing more the should I have heaven, <br />
Forward, therefore as ourself in the business it, Horatio <br />
To what is't that your watch, bid this here! </p>
</blockquote>
<p>Usage :</p>
<pre class="prettyprint"><code>generator = TextGenerator(txt)
result = generator.random_text(3000)
</code></pre>
Encrypt and Decrypt Text and Text Files (BETA) (Python)
2011-11-22T04:18:15-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577954-encrypt-and-decrypt-text-and-text-files-beta/
<p style="color: grey">
Python
recipe 577954
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/cryptography/">cryptography</a>, <a href="/recipes/tags/cryptology/">cryptology</a>, <a href="/recipes/tags/decrypt/">decrypt</a>, <a href="/recipes/tags/decryption/">decryption</a>, <a href="/recipes/tags/encrypt/">encrypt</a>, <a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/hidding/">hidding</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/text_files/">text_files</a>).
Revision 7.
</p>
<p>This program interactively lets you encrypt and decrypt text as well as text files using a key system as well as a cipher.</p>
Encrypt and Decrypt Text and Text Files (BETA) Jython GUI (Python)
2011-11-23T05:37:04-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577957-encrypt-and-decrypt-text-and-text-files-beta-jytho/
<p style="color: grey">
Python
recipe 577957
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/cryptography/">cryptography</a>, <a href="/recipes/tags/cryptology/">cryptology</a>, <a href="/recipes/tags/decryption/">decryption</a>, <a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/encryption_decryption/">encryption_decryption</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/hiding/">hiding</a>, <a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p>This is a Jython GUI of my previous recipe: Encrypt and Decrypt Text and Text Files</p>
Word Wrap for Proportional Fonts (Python)
2011-11-11T21:29:21-08:00Michael Foglemanhttp://code.activestate.com/recipes/users/4171845/http://code.activestate.com/recipes/577946-word-wrap-for-proportional-fonts/
<p style="color: grey">
Python
recipe 577946
by <a href="/recipes/users/4171845/">Michael Fogleman</a>
(<a href="/recipes/tags/fixed/">fixed</a>, <a href="/recipes/tags/font/">font</a>, <a href="/recipes/tags/proportional/">proportional</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/word/">word</a>, <a href="/recipes/tags/word_wrap/">word_wrap</a>, <a href="/recipes/tags/wrap/">wrap</a>).
</p>
<p>Word wrap function / algorithm for wrapping text using proportional (versus
fixed-width) fonts.</p>
<p><code>text</code>: a string of text to wrap
<code>width</code>: the width in pixels to wrap to
<code>extent_func</code>: a function that returns a (w, h) tuple given any string, to
specify the size (text extent) of the string when rendered.
the algorithm only uses the width.</p>
<p>Returns a list of strings, one for each line after wrapping.</p>
Table indentation (Python)
2011-03-18T14:39:59-07:00Jonathan Blakeshttp://code.activestate.com/recipes/users/4177368/http://code.activestate.com/recipes/577615-table-indentation/
<p style="color: grey">
Python
recipe 577615
by <a href="/recipes/users/4177368/">Jonathan Blakes</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>A function for pretty-printing a table.</p>
Convert a transcript with Ansi escape sequences to HTML (Python)
2010-10-09T22:29:42-07:00Muhammad Alkarourihttp://code.activestate.com/recipes/users/4049383/http://code.activestate.com/recipes/577349-convert-a-transcript-with-ansi-escape-sequences-to/
<p style="color: grey">
Python
recipe 577349
by <a href="/recipes/users/4049383/">Muhammad Alkarouri</a>
(<a href="/recipes/tags/ansi/">ansi</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>This is a stab at converting a transcript generated by the Unix <code>script</code> command that uses ANSI escape sequences, used to colour the terminal, to HTML.</p>
Render tables for text interface (Python)
2010-04-20T18:02:51-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577202-render-tables-for-text-interface/
<p style="color: grey">
Python
recipe 577202
by <a href="/recipes/users/57155/">Denis Barmenkov</a>
(<a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/table/">table</a>, <a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>Sometime pprint module is not enough for formatting data for console or log file output.
This module provide function which fill the gap.</p>
<p><strong>Sample function call:</strong></p>
<pre class="prettyprint"><code>nums = [ '1', '2', '3', '4' ]
speeds = [ '100', '10000', '1500', '12' ]
desc = [ '', 'label 1', 'none', 'very long description' ]
lines = format_table( [(nums, ALIGN_RIGHT|PADDING_ALL, 'NUM'),
(speeds, ALIGN_RIGHT|PADDING_ALL, 'SPEED'),
(desc, ALIGN_LEFT|PADDING_ALL, 'DESC')] )
</code></pre>
<p><strong>Output:</strong></p>
<pre class="prettyprint"><code>=======================================
| NUM | SPEED | DESC |
=======================================
| 1 | 100 | |
| 2 | 10000 | label 1 |
| 3 | 1500 | none |
| 4 | 12 | very long description |
=======================================
</code></pre>