Most viewed recipes tagged "format"http://code.activestate.com/recipes/tags/format/views/2016-05-05T23:47:49-07:00ActiveState Code RecipesConvert HTML text to PDF with Beautiful Soup and xtopdf (Python)
2015-01-28T22:20:53-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579014-convert-html-text-to-pdf-with-beautiful-soup-and-x/
<p style="color: grey">
Python
recipe 579014
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/format/">format</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/reportgeneration/">reportgeneration</a>, <a href="/recipes/tags/reporting/">reporting</a>, <a href="/recipes/tags/reportlab/">reportlab</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>).
</p>
<p>This recipe shows how to convert the text in an HTML document to PDF. It uses the Beautiful Soup and xtopdf Python libraries. Beautiful Soup is a library for HTML parsing and content extraction. xtopdf is a library for PDF creation from other formats, including text and many others.</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>
Find Paper Format of based on Pixel Width and Height of a (PDF) Page (Python)
2016-05-05T23:47:49-07:00Harald Liederhttp://code.activestate.com/recipes/users/4191581/http://code.activestate.com/recipes/580659-find-paper-format-of-based-on-pixel-width-and-heig/
<p style="color: grey">
Python
recipe 580659
by <a href="/recipes/users/4191581/">Harald Lieder</a>
(<a href="/recipes/tags/a4/">a4</a>, <a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/letter/">letter</a>, <a href="/recipes/tags/paper/">paper</a>).
</p>
<p>A simple function to determine what format page a page in document has. Parameters provided are width and height in float or integer format.</p>
<p>Return is a string like "A4-P" or "Letter-L" when an exact fit is found. If not, information is provided as a string like "width x height (other), closest <format> width x height". The closest format in this case is determined by minimizing the sum of absolute differences of width and height with a table of official formats.</p>
Formating strings (print a table) (Batch)
2013-06-18T07:52:03-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578562-formating-strings-print-a-table/
<p style="color: grey">
Batch
recipe 578562
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/table/">table</a>).
</p>
<p>As you know there are no escape characters such as "\t" in the windows command language but it does not mean that we can not format text. Command prompt has its own tricks. At firstly, you need declare enabledelayedexpansion after setlocal command in your batch file to get access for some interesting things; secondly, use <code><nul set /p "str=[string]"</code> construction which is equal print function in C language. OK, next batch file print multiplication table.</p>
Convert Image Format (Python)
2011-09-30T10:46:21-07:00s_h_a_i_ohttp://code.activestate.com/recipes/users/4177334/http://code.activestate.com/recipes/577886-convert-image-format/
<p style="color: grey">
Python
recipe 577886
by <a href="/recipes/users/4177334/">s_h_a_i_o</a>
(<a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/image/">image</a>).
Revision 2.
</p>
<p>Simple GUI to allow converting images from one format to another. Available formats are: .gif .jpg .png .tif .bmp Uses PIL.</p>
<p>This is short and direct enhancement from recipe <a href="http://code.activestate.com/recipes/180801-convert-image-format" rel="nofollow">http://code.activestate.com/recipes/180801-convert-image-format</a>. Two new features added:</p>
<ul>
<li>Optional deletion of the original image files.</li>
<li>Optional recursive file selection. Implemented using <a href="http://code.activestate.com/recipes/577230-file-path-generator-from-path-patterns/" rel="nofollow">http://code.activestate.com/recipes/577230-file-path-generator-from-path-patterns/</a></li>
</ul>
<p><strong>note</strong> This assumes recipe/577230 is located in filePattern.py (see first import below)</p>
format a number for human consumption (Python)
2008-07-29T07:06:58-07:00Pádraig Bradyhttp://code.activestate.com/recipes/users/1890175/http://code.activestate.com/recipes/576385-format-a-number-for-human-consumption/
<p style="color: grey">
Python
recipe 576385
by <a href="/recipes/users/1890175/">Pádraig Brady</a>
(<a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/iec/">iec</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/si/">si</a>, <a href="/recipes/tags/thousands/">thousands</a>).
</p>
<p>convert a number to a string representation that is more easily parsed by humans.
It supports inserting thousands separators and converting to IEC or SI units.</p>
Cheap-date trick; a different way to parse (Python)
2012-03-06T14:08:10-08:00Scott S-Allenhttp://code.activestate.com/recipes/users/4181178/http://code.activestate.com/recipes/578064-cheap-date-trick-a-different-way-to-parse/
<p style="color: grey">
Python
recipe 578064
by <a href="/recipes/users/4181178/">Scott S-Allen</a>
(<a href="/recipes/tags/cheap/">cheap</a>, <a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/sharp/">sharp</a>).
</p>
<p>... a light meal with a heavy dose of "tutorial mash" on the side.</p>
<p>In the constructive spirit of "more ways to solve a problem"; this is a portion of my lateral, occasionally oblique, solutions. Nothing new in le régime de grande, but hopefully the conceptual essence will amuse.</p>
<p>Initially started as a response to <a href="http://code.activestate.com/recipes/577135/">recipe 577135</a> which parses incremental date fragments and preserves micro-seconds where available. That script does more work than this, for sure, but requires special flow-control and iterates a potentially incumbering shopping list (multi-dimensional with some detail).</p>
<p>So here's a different box for others to play with. Upside-down in a sense, it doesn't hunt for anything but a numerical "pulse"; sequences of digits punctuated by other 'stuff' we don't much care about.</p>
<p>Missing a lot of things, intentionally, this snippet provides several examples demoin' flexibility. Easy to button-up, redecorate and extend later for show, till then the delightful commentary makes it hard enough to see bones already -- all six lines or so!</p>
<p><strong>Note:</strong> <em>The core script is repeated for illustrative purposes. The first is step-by-step, the second is lean and condensed for utilitarian purposes. It is the second, shorter, version that I yanked from a file and gussied up.</em></p>