Posts Tagged ‘linux’

Read Excel files from Python

Wednesday, April 9th, 2008

Use the excellent xlrd package, which works on any platform. That means you can read Excel files from Python in Linux! Example usage:

Open the workbook

import xlrd
wb = xlrd.open_workbook('myworkbook.xls')

Check the sheet names

wb.sheet_names()

Get the first sheet either by index or by name

sh = wb.sheet_by_index(0)
sh = wb.sheet_by_name(u'Sheet1')

Iterate through rows, returning each as a list that you can index:

for rownum in range(sh.nrows):
    print sh.row_values(rownum)

If you just want the first column:

first_column = sh.col_values(0)

Index individual cells:

cell_A1 = sh.cell(0,0).value
cell_C4 = sh.cell(rowx=3,colx=2).value

(Note Python indices start at zero but Excel starts at one)

Turns out the put_cell() method isn’t supported, so ignore the following section (Thanks for the heads up, John!)

Put something in the cell:

row = 0
col = 0
ctype = 1  # see below
value = 'asdf'
xf = 0  # extended formatting (use 0 to use default)
sh.put_cell(row, col, ctype, value, xf)
sh.cell(0,0)  # text:u'asdf'
sh.cell(0,0).value  # 'asdf'

Possible ctypes: 0 = empty, 1 = string, 2 = number, 3 = date, 4 = boolean, 5 = error

Check open ports

Thursday, March 13th, 2008

Are there any open ports that shouldn’t be open? Check with:

sudo netstat -tupl

Results in something like:


Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address    Foreign Address  State    PID/Program name
tcp        0      0 localhost:mysql  *:*              LISTEN  5077/mysqld
tcp        0      0 localhost:ipp    *:*              LISTEN  5001/cupsd
udp        0      0 *:32768          *:*                      5324/avahi-daemon:
udp        0      0 *:bootpc         *:*                      5875/dhclient
udp        0      0 *:mdns           *:*                      5324/avahi-daemon: 

Kill the process that is using the port

kill (PID here)

List open files:

lsof -i

where the -i makes it list internet files.

Split the pages out of a PDF, or merge PDFs into one

Sunday, January 27th, 2008

Using the free open source program PDF Toolkit, you can operate on PDFs quite easily. (more…)