def FindMergedCells():
myfile = '02-Sep-2015.xlsx'
from openpyxl import load_workbook
wb = load_workbook(filename = myfile)
sheet = wb.get_active_sheet()
#print(sorted(sheet.merged_cell_ranges))
for items in sorted(sheet.merged_cell_ranges):
print(items)
itemLength = ((len(items) - 1)/2)
print (itemLength)
cell1 = items[0:itemLength]
print (cell1)
print sheet[cell1].value
sheet.unmerge_cells(str(items) )
row = (items)
for cell in row: value = sheet[cell1].value
wb.save(myfile)
print(sorted(sheet.merged_cell_ranges))
print('successful') row = (items)
for cell in row: value = sheet[cell1].valuedef FindMergedCells():
myfile = '02-Sep-2015.xlsx'
from openpyxl import load_workbook
wb = load_workbook(filename = myfile)
sheet = wb.get_active_sheet()
for items in sorted(sheet.merged_cell_ranges):
itemLength = ((len(items) - 1)/2) # this line returns the number of characters to use in the slice next line down to get the first cell of the merged cells
)
cell1 = items[0:itemLength] # this line returns the slice of "A100" from "A100:P100" for example, so cell1 now points to the cell holding the value in the merged cells
sheet.unmerge_cells(str(items) ) # this unmerges the cells
row = (items) # these two lines were an
for cell in row: value = sheet[cell1].value # attempt to to write the value from the merged cells back to the now unmerged cells in the range A100:P100
wb.save(myfile)
def unlock_Cells(myfile):
wb = load_workbook(filename = myfile)
ws = wb.active
ws.protection.sheet = False
for row in ws.iter_rows('A1:T50'):
for cell in row:
if cell.style.protection.locked:
protection = Protection(locked=False, hidden=False)
def findMergedCells(myfile):
wb = load_workbook(filename = myfile)
#wb = load_workbook()
ws = wb.active
for cell_range in ws.merged_cell_ranges:
print (ws.merged_cell_ranges)
coord = cell_range.split(":")[0]
value = ws[coord].value
ws.unmerge_cells(cell_range)
for row in ws[cell_range]:
for cell in row:
cell.value = value
wb.save(myfile)
def findMergedCells(myfile):
wb = load_workbook(filename = myfile)
ws = wb.active
while ws.merged_cell_ranges != []:
for cell_range in ws.merged_cell_ranges:
print (ws.merged_cell_ranges)
coord = cell_range.split(":")[0]
value = ws[coord].value
ws.unmerge_cells(cell_range)
for row in ws[cell_range]:
for cell in row:
cell.value = value
wb.save(myfile)