python: update dataframe to existing excel sheet without overwriting contents on the same sheet and other sheets

Figured it out by myself: #Prepare the excel we want to write to t=pd.date_range(‘2004-01-31′, freq=’M’, periods=4) first=pd.DataFrame({‘A’:[1,1,1,1], ‘B’:[2,2,2,2]}, index=t) first.index=first.index.strftime(‘%Y-%m-%d’) writer=pd.ExcelWriter(‘test.xlsx’) first.to_excel(writer, sheet_name=”Here”) first.to_excel(writer, sheet_name=”Keep”) #read the existing sheets so that openpyxl won’t create a new one later book = load_workbook(‘test.xlsx’) writer = pandas.ExcelWriter(‘test.xlsx’, engine=”openpyxl”) writer.book = book writer.sheets = dict((ws.title, ws) for ws in … Read more

Identifying Excel Sheet cell color code using XLRD package

Here is one way to handle this: import xlrd book = xlrd.open_workbook(“sample.xls”, formatting_info=True) sheets = book.sheet_names() print “sheets are:”, sheets for index, sh in enumerate(sheets): sheet = book.sheet_by_index(index) print “Sheet:”, sheet.name rows, cols = sheet.nrows, sheet.ncols print “Number of rows: %s Number of cols: %s” % (rows, cols) for row in range(rows): for col in … Read more

Insert row into Excel spreadsheet using openpyxl in Python

== Updated to a fully functional version, based on feedback here: groups.google.com/forum/#!topic/openpyxl-users/wHGecdQg3Iw. == As the others have pointed out, openpyxl does not provide this functionality, but I have extended the Worksheet class as follows to implement inserting rows. Hope this proves useful to others. def insert_rows(self, row_idx, cnt, above=False, copy_style=True, fill_formulae=True): “””Inserts new (empty) rows … Read more

Python xlwt – accessing existing cell content, auto-adjust column width

I just implemented a wrapper class that tracks the widths of items as you enter them. It seems to work pretty well. import arial10 class FitSheetWrapper(object): “””Try to fit columns to max size of any entry. To use, wrap this around a worksheet returned from the workbook’s add_sheet method, like follows: sheet = FitSheetWrapper(book.add_sheet(sheet_name)) The … Read more

How to obtain sheet names from XLS files without loading the whole file?

you can use the xlrd library and open the workbook with the “on_demand=True” flag, so that the sheets won’t be loaded automaticaly. Than you can retrieve the sheet names in a similar way to pandas: import xlrd xls = xlrd.open_workbook(r'<path_to_your_excel_file>’, on_demand=True) print xls.sheet_names() # <- remeber: xlrd sheet_names is a function, not a property

Convert date from excel in number format to date format python [duplicate]

from datetime import datetime excel_date = 42139 dt = datetime.fromordinal(datetime(1900, 1, 1).toordinal() + excel_date – 2) tt = dt.timetuple() print(dt) print(tt) As mentioned by J.F. Sebastian, this answer only works for any date after 1900/03/01 EDIT: (in answer to @R.K) If your excel_date is a float number, use this code: from datetime import datetime def … Read more