p_s*_*and 4 python python-2.7 python-3.x pandas xlsxwriter
感觉应该有一种方法可以使用 pandas 的to_excel函数或XLSXwriter模块将 .xlsx 文件保存为只读文件。
我在没有运气的情况下查看了两个文档。
to_excel: https://pandas.pydata.org/pandasdocs/stable/generated/pandas.DataFrame.to_excel.html
XLSXwriter:http : //xlsxwriter.readthedocs.io/workbook.html
有没有另一种方法可以在熊猫中实现这一目标?
小智 5
不确定它是否可以用 Pandas 完成,但您可以在创建它后将其设置为只读。
import os
from stat import S_IREAD, S_IRGRP, S_IROTH
os.chmod(filename, S_IREAD|S_IRGRP|S_IROTH)
Run Code Online (Sandbox Code Playgroud)
以下是使用 xlsxwriter 作为引擎时使用 Pandas 执行此操作的一种方法:
import pandas as pd
# Create a Pandas dataframe from some data.
df = pd.DataFrame({"Data": [10, 20, 30, 20, 15, 30, 45]})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter("pandas_read_only.xlsx", engine="xlsxwriter")
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name="Sheet1")
# Set the Excel output file as "Read-only Recommended".
writer.book.read_only_recommended()
# Close the Pandas Excel writer and output the Excel file.
writer.close()
Run Code Online (Sandbox Code Playgroud)
打开文件时的输出: