Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to save .xlsx as read-only using pandas?

It feels like there should be a way to save a .xlsx file as read-only using pandas' to_excel function or the XLSXwriter module.

I have looked at both documentations without luck.

to_excel: https://pandas.pydata.org/pandasdocs/stable/generated/pandas.DataFrame.to_excel.html

XLSXwriter: http://xlsxwriter.readthedocs.io/workbook.html

Is there another way to achieve this within pandas?

like image 796
p_sutherland Avatar asked Jan 03 '23 02:01

p_sutherland


2 Answers

Not sure if it can be done with pandas but you can set it to read only after you create it.

import os
from stat import S_IREAD, S_IRGRP, S_IROTH
os.chmod(filename, S_IREAD|S_IRGRP|S_IROTH)
like image 87
Alex Zisman Avatar answered Jan 05 '23 14:01

Alex Zisman


Still, if you want to use XLSXwriter

from xlsxwriter.workbook import Workbook
...
book = Workbook('file/path')
sheet = book.add_worksheet('worksheet_name')

# Add separate format for unlocked cells
unlocked = book.add_format({'locked': 0})
# Protect all cells in your sheet by default
sheet.protect()
...
# Write cell, locked by default
sheet.write(row_number, column_number, data)
# Write another cell, unlock it
sheet.write(row_number, column_number, data, unlocked)
...
book.close()
like image 40
Yaroslav Varkhol Avatar answered Jan 05 '23 15:01

Yaroslav Varkhol