Friday, March 30, 2018

Python docx Highlight Matching Text in Microsoft Word Doc

"""Usage : python /home/you/script/this.py file.docx 'phrase to highlight'  # notice the quotes
"""

from docx import Document
from docx.enum.text import WD_COLOR_INDEX
import sys

source = sys.argv[1]
phrase = " ".join(sys.argv[2:]).strip("'")

doc = Document( source )

for para in doc.paragraphs :
start = para.text.find( phrase )
if start > -1 :
pre = para.text[:start]
post = para.text[start+len(phrase):]
para.text = pre
para.add_run(phrase)
para.runs[1].font.highlight_color = WD_COLOR_INDEX.YELLOW
para.add_run(post)

doc.save( source )

Python docx Tutorial

Don't install docx, install python-docx

$ pip install python-docx

>>> from docx import Document

Then, a basic one is :

doc = Document.('docName.docx')
for para in doc.paragraphs :
   print para.text

How should you know that you have paragraphs and text for each para? You can use the TAB key when you've typed "doc." (without the quotes, of course).

Search and Highlight (Permanently) in Microsoft Word

You want to be productive - you have a phrase (or word) that appears multiple times in your doc, and you want to highlight all occurrences.

CTRL-h : launches the Find and Replace dialog

Click on More to get to more options to allow you to specify what format to use for the replacement.


Fill in the fields - unfortunately, Word hasn't heard of NEdit or vi and you can't even do simple things like regex and replace-with-portion-of-match, etc..


Click on Replace All and then, enjoy!!



Thursday, March 22, 2018

How Useful is Perl's -l? It Depends

You want to generate

1
2
3
4
5

If you didn't know about -l, you might do

$ perl -e 'foreach (1..5) {print; print "\n";}'

But, if you knew about -l, you could do :

$ perl -le 'foreach (1..5) {print;}'

To accomplish the same thing. Moral - ... make up your own moral. But, never stop learning!!

Now, what if you wanted to pipe this through a script to produce just

1

One way is

| perl -n -e 'print and exit if /\d/;'

Works, but doesn't

| perl -n -e 'print if ?\d?;'

Look prettier? What is the ?? version of the match operator? Only match once!

Tuesday, March 20, 2018

M$ Excel : How to Capture the Max of Only the Displayed Values of a Column in a Table

MAX is not your friend. SUBTOTAL is. How should you know that? Don't ask me.

So, you have your beautiful data and, you've done the Format as Table trick.

Now, you can quickly do filtering on it - to see how good/bad things are under different conditions.

Thing is, can you have a cell somewhere up top that shows you the MAX of your table, and uses ONLY THE DISPLAYED VALUES. Else, what's the point. Right?

Thanks to these (https://exceljet.net/excel-functions/excel-subtotal-function) guys, here's what you do :

You start typing : =subtotal

You'll get prompted by Excel with SUBTOTAL. Hit Tab key to select that and now, you have to choose the function. If you refer above page, you'll find that the version of the function whose number has "10" in front (i.e., 101 vs 1) ignores hidden values. So, for MAX, it's 104. Thankfully, Excel gives you a gorgeous drop-down menu that makes life super easy.

So, if you've named your table (good idea:), you'll end up with something like

=SUBTOTAL( 104, myTable[@col_name])

And it'll work great!!

Tuesday, March 13, 2018

Power Aus Perl Debugger (Conditional Breaking)

If you're bummed out by (say)

Use of uninitialized value $whatever in print at ...
.. .blah blah ..
         DB::DB called at /home/you/perl/whatever.pl line 222

What you need is a dose of 

DB<1> b 113 !defined( $whatever )

Gets you where you want to go faster :)


Thursday, March 08, 2018

Allen Downey Ain't Perfect

But he must be ein genius if he can see what his draw program does just by looking at it. Urm... maybe if you worked out the turtle examples in his ch.4 you'd be okay :) (I didn't)

He must not be aware of repl.it/languages/python_turtle -- makes life so simple. With that in mind, here's his draw() example :

import turtle
def draw(t, length, n):
if n == 0:
return
angle = 50
t.forward( length*n)
t.left(angle)
draw(t, length, n-1)
t.right( 2*angle)
draw(t, length, n-1)
t.left( angle)
t.back( length*n)

t = turtle.Turtle()
draw( t, 10, 6)

So cute, and an intro to fractals too :)

Tuesday, March 06, 2018

Gems Aus Allen Downey

One of the attractions of complexity is that the research frontier is accessible with moderate programming skills and undergraduate mathematics.

In 2009-10 I was a Visiting Scientist at Google, working in their Cambridge office. One of the things that impressed me about the software engineers I worked with was their broad intellectual curiosity and drive to expand their knowledge and skills

Sunday, March 04, 2018

Python : Pretty Print a Table

That is, if you've set up a board
[ ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'] ]

How do you get

O O O O O
..
O O O O O

You get the idea..

def print_board(board_in ) :
  for row in board_in :
    print " ".join(row)

Is one way out..

Per https://stackoverflow.com/questions/13214809/pretty-print-2d-python-list

these may also be of help :

from prettytable import PrettyTable

x = [["A", "B"], ["C", "D"]]

p = PrettyTable()
for row in x:
    p.add_row(row)

print p.get_string(header=False, border=False)

from pandas import *
x = [["A", "B"], ["C", "D"]]
print DataFrame(x)

   0  1
0  A  B
1  C  D
Python pretty print a dict :

pprint.pprint( dict ) will give you :
>>> pprint.pprint( transition_counts )
{('NN', 'NN'): 16241,
 ('NN', 'RB'): 2431,
 ('NN', 'TO'): 5256,
 ('RB', 'NN'): 358,
 ('RB', 'RB'): 2263,
 ('RB', 'TO'): 855,
 ('TO', 'NN'): 734,
 ('TO', 'RB'): 200,
 ('TO', 'TO'): 2}

Is This Idiomatic Python?

if isInstance( varName, list ) :

versus

if list == type( varName )

Just asking..

Python : Find Index of Matching Element in List

Something simple, like

l = ['a', 'b', 'c']

i_c = l.index( 'c' )

Shame that codecademy doesn't tell you you can use [-2:] to retrieve the last two elements..

Paul Barry - Head First Python : Resources

http://www.headfirstlabs.com/books/hfpython/
http://python.itcarlow.ie