Get formula from Excel cell with python xlrd

[Dis]claimer: I’m the author/maintainer of xlrd. The documentation references to formula text are about “name” formulas; read the section “Named references, constants, formulas, and macros” near the start of the docs. These formulas are associated sheet-wide or book-wide to a name; they are not associated with individual cells. Examples: PI maps to =22/7, SALES maps … Read more

Reading Excel File using Python, how do I get the values of a specific column with indicated column name?

A somewhat late answer, but with pandas, it is possible to get directly a column of an excel file: import pandas df = pandas.read_excel(‘sample.xls’) #print the column names print df.columns #get the values for a given column values = df[‘Arm_id’].values #get a data frame with selected columns FORMAT = [‘Arm_id’, ‘DSPName’, ‘Pincode’] df_selected = df[FORMAT] … Read more

writing to existing workbook using xlwt [closed]

Here’s some sample code I used recently to do just that. It opens a workbook, goes down the rows, if a condition is met it writes some data in the row. Finally it saves the modified file. from xlutils.copy import copy # http://pypi.python.org/pypi/xlutils from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd START_ROW = 297 # 0 based … Read more

Preserving styles using python’s xlrd,xlwt, and xlutils.copy

There are two parts to this. First, you must enable the reading of formatting info when opening the source workbook. The copy operation will then copy the formatting over. import xlrd import xlutils.copy inBook = xlrd.open_workbook(‘input.xls’, formatting_info=True) outBook = xlutils.copy.copy(inBook) Secondly, you must deal with the fact that changing a cell value resets the formatting … Read more

Create a .csv file with values from a Python list

import csv with open(…, ‘wb’) as myfile: wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) wr.writerow(mylist) Edit: this only works with python 2.x. To make it work with python 3.x replace wb with w (see this SO answer) with open(…, ‘w’, newline=””) as myfile: wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) wr.writerow(mylist)

GroupBy results to dictionary of lists

You could groupby on Column1 and then take Column3 to apply(list) and call to_dict? In [81]: df.groupby(‘Column1’)[‘Column3’].apply(list).to_dict() Out[81]: {0: [1], 1: [2, 3, 5], 2: [1, 2], 3: [4, 5], 4: [1], 5: [1, 2, 3]} Or, do In [433]: {k: list(v) for k, v in df.groupby(‘Column1’)[‘Column3’]} Out[433]: {0: [1], 1: [2, 3, 5], 2: … Read more