Ajay
Ajay

Reputation: 45

How do I write Excel sheet without protected it?

I got an Excel sheet, which included some protected cells. I used python program to write some other cells, then the cells were all protected. I dont want to protect the cells I write. How should I do? Thanks.

wb = openpyxl.load_workbook('SecGen.xlsx')
ws = wb['5.1 Frame.Info']
cell = ws['K1']
cell.value = 'Oi, cm'
cell = ws['L1']
cell.value = 'Oj, cm'
for i in range(0, len(ori_e2k.line_se_point_list), 1):
    for j in range(0, len(ori_e2k.line_se_point_list[i]), 1):
        cell = ws.cell(row=i+2, column=j+2)
        cell.value = ori_e2k.line_se_point_list[i][j]
wb.close()

Upvotes: 0

Views: 168

Answers (1)

Ajay
Ajay

Reputation: 45

openpyxl default enables protection. Just disable it.

import openpyxl
from openpyxl.styles import Protection, Font
...
...
wb = openpyxl.load_workbook('SecGen.xlsx')
ws = wb['5.1 Frame.Info']
font = Font(name='Calibri')
cell = ws['K1']
cell.value = 'Oi, cm'
cell.protection = Protection(locked=False)
cell.font = Font(name='Calibri')
cell = ws['L1']
cell.value = 'Oj, cm'
cell.protection = Protection(locked=False)
cell.font = Font(name='Calibri')
for i in range(0, len(ori_e2k.line_se_point_list), 1):
    for j in range(0, len(ori_e2k.line_se_point_list[i]), 1):
        cell = ws.cell(row=i+2, column=j+2)
        cell.value = ori_e2k.line_se_point_list[i][j]
        cell.protection = Protection(locked=False)
        cell.font = Font(name='Calibri')
wb.save('SecGen.xlsx')
wb.close()

Upvotes: 1

Related Questions