copy cell style openpyxl

As of openpyxl 2.5.4, python 3.4: (subtle changes over the older version below) new_sheet = workbook.create_sheet(sheetName) default_sheet = workbook[‘default’] from copy import copy for row in default_sheet.rows: for cell in row: new_cell = new_sheet.cell(row=cell.row, column=cell.col_idx, value= cell.value) if cell.has_style: new_cell.font = copy(cell.font) new_cell.border = copy(cell.border) new_cell.fill = copy(cell.fill) new_cell.number_format = copy(cell.number_format) new_cell.protection = copy(cell.protection) new_cell.alignment … Read more

Iterate over Worksheets, Rows, Columns

Read the OpenPyXL Documentation Iteration over all worksheets in a workbook, for instance: for n, sheet in enumerate(wb.worksheets): print(‘Sheet Index:[{}], Title:{}’.format(n, sheet.title)) Output: Sheet Index:[0], Title: Sheet Sheet Index:[1], Title: Sheet1 Sheet Index:[2], Title: Sheet2 Iteration over all rows and columns in one Worksheet: worksheet = workbook.get_sheet_by_name(‘Sheet’) for row_cells in worksheet.iter_rows(): for cell in row_cells: … Read more

How to save a new sheet in an existing excel file, using Pandas?

Thank you. I believe that a complete example could be good for anyone else who have the same issue: import pandas as pd import numpy as np path = r”C:\Users\fedel\Desktop\excelData\PhD_data.xlsx” x1 = np.random.randn(100, 2) df1 = pd.DataFrame(x1) x2 = np.random.randn(100, 2) df2 = pd.DataFrame(x2) writer = pd.ExcelWriter(path, engine=”xlsxwriter”) df1.to_excel(writer, sheet_name=”x1″) df2.to_excel(writer, sheet_name=”x2″) writer.save() writer.close() Here … Read more