Hello,
This is my first post, be gentle. :)
I am an expert Excel user who is now venturing into Python in an effort to automate "the boring stuff".
That said, I have what is probably a very basic question, however, I have come to a dead end. Via Python (openpyxl) I would like to cut and paste (values only) one specific tab into a new Excel file. The code I am using below works well in terms of only pasting values and preserving formatting. The downfall is it copies ALL the tabs of the source file.
I am calling out the desired tab name that I am trying to copy ('10_Yr') in line five of said code (shown below), however, it doesn't seem to work. How can I specify only the exact tab(s) that I want to copy? Every search and tutorial that I encounter somehow doesn't address this. Many thanks in advance.
This is my first post, be gentle. :)
I am an expert Excel user who is now venturing into Python in an effort to automate "the boring stuff".
That said, I have what is probably a very basic question, however, I have come to a dead end. Via Python (openpyxl) I would like to cut and paste (values only) one specific tab into a new Excel file. The code I am using below works well in terms of only pasting values and preserving formatting. The downfall is it copies ALL the tabs of the source file.
I am calling out the desired tab name that I am trying to copy ('10_Yr') in line five of said code (shown below), however, it doesn't seem to work. How can I specify only the exact tab(s) that I want to copy? Every search and tutorial that I encounter somehow doesn't address this. Many thanks in advance.
from openpyxl import load_workbook from copy import copy wb = load_workbook(r"C:\Original File Name.xlsx", data_only = True) #your file name ws = wb['10_Yr'] #your sheet name in the file above for group in list(ws.merged_cells.ranges): min_col, min_row, max_col, max_row = group.bounds cell_start = ws.cell(row = min_row, column = min_col) top_left_cell_value = cell_start.value ws.unmerge_cells(str(group)) for i_row in range(min_row, max_row + 1): for j_col in range(min_col, max_col + 1): ws.cell(row = i_row, column = j_col, value = top_left_cell_value) #copy the cell format ws.cell(row = i_row, column = j_col).alignment = copy(cell_start.alignment) ws.cell(row = i_row, column = j_col).border = copy(cell_start.border) ws.cell(row = i_row, column = j_col).font = copy(cell_start.font) wb.save(r"C:\new file name.xlsx")