python
python中xlwt的局限,python xlwt 生成excel和设置特定单元格不可编辑 -凯发ag旗舰厅登录网址下载
请看我另一篇生成excel的文章(推荐)
xlwt
这是一个供开发人员用来生成与microsoft excel 95到2003版兼容的电子表格文件的库。
包本身是纯python,不依赖于标准python发行版之外的模块或包。
安装
pip install xlwt
文档
示例
import xlwt
import sys
ezxf = xlwt.easyxf
# 生成excel文件
# data:数据
# encoding:编码
# sheet:工作表名称
# path:保存路径
# filename:文件名
# protect:是否开启保护
# password:保护密码
def save_excel(data, encoding, sheet, path, filename, protect, password):
try:
# 创建一个workbook 设置编码
workbook = xlwt.workbook(encoding=encoding)
# 创建一个worksheet
worksheet = workbook.add_sheet(sheet)
worksheet.protect = protect # 設置保護
worksheet.password = password # 設置密碼
# 可编辑,horz center:水平居中,vert center:垂直居中,还可以在这里设置其他样式,颜色,边框等
editable = ezxf("protection: cell_locked false;align: horz center,vert center;")
# 不可編輯,horz center:水平居中,vert center:垂直居中,还可以在这里设置其他样式,颜色,边框等
read_only = ezxf("protection: cell_locked true;align: horz center,vert center;")
row = 0 # 行
col = 0 # 列
for item in data:
for k, v in item.items():
# 写入excel
# 参数对应 行, 列, 值
if k == "id" or k == "name" or k == "field_id" or k == "field_name":
worksheet.write(row, col, label=v, style=read_only) # 不可编辑
else:
worksheet.write(row, col, label=v, style=editable) # 可以编辑
col = 1
col = 0
row = 1
# 保存
workbook.save("{path}{filename}".format(path=path, filename=filename))
except exception as e:
print("[error]: {error}".format(error=e))
else:
print("[success]: {path}{filename}".format(path=path, filename=filename))
if __name__ == '__main__':
data = [
{"id": "1", "name": "小明", "field_id": "005", "field_name": "005", "date": "2019-7-20", "morning_to_work": "10:00", "morning_off_work": "13:00", "afternoon_to_work": "14:00",
"afternoon_off_work": "19:00", "to_overtime": "20:00", "off_overtime": "22:00", "overtime_hours": "3"},
{"id": "2", "name": "小红", "field_id": "006", "field_name": "006", "date": "2019-7-21", "morning_to_work": "10:00", "morning_off_work": "13:00", "afternoon_to_work": "14:00",
"afternoon_off_work": "19:00", "to_overtime": "20:00", "off_overtime": "22:00", "overtime_hours": "3"},
]
save_excel(data=data, encoding="utf-8", sheet="sheet1", path="/xxx/xxx/xxx/", filename='xxx.xlsx', protect=true, password="xxx")
编辑受保护的单元格
受保护的单元格
编辑不受保护的单元格
不受保护的单元格
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的python中xlwt的局限,python xlwt 生成excel和设置特定单元格不可编辑的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇: 基于matlab的智能天线波束方向图仿真
- 下一篇: angularjs 导出excel ph