辉煌仪奇 2022-01-22 11:00 采纳率: 47.4%
浏览 21
已结题

python excel 创建密码保护失败

报错如下

img

代码如下

def create_project_excel(list1: [[]], list2: [[]], name: str, filenamepassword: str) -> None:
    import pythoncom
    pythoncom.CoInitialize()
    import win32com.client as win32
    excel = win32.Dispatch('Excel.Application')
    excel.Visible = False  # 可见
    wb1 = excel.Workbooks.Add()  # 创建excel
    wb1.Worksheets.Add().Name = '汇总'  # 命名初始sheet
    ws1 = wb1.Worksheets('汇总')
    ws1.Range('A1:J2').Merge()
    ws1.Range(f'A1:J{len(list1) + len(list2) + 7}').HorizontalAlignment = -4108
    ws1.Range('A3:J3').Value = [['序号', '产品名称', '品牌', '型号', '参数', '单位', '数量', '单价', '合计', '备注']]
    ws1.Range(f'A4:J{len(list1) + 3}').Value = list1
    ws1.Range(f'A{len(list1) + 6}:J{len(list1) + len(list2) + 5}').Value = list2
    ws1.Range(f'A{len(list1) + 4}:H{len(list1) + 4}').Merge()
    ws1.Range(f'A{len(list1) + 5}:J{len(list1) + 5}').Merge()
    ws1.Range(f'A{len(list1) + len(list2) + 6}:H{len(list1) + len(list2) + 6}').Merge()
    ws1.Range(f'A{len(list1) + len(list2) + 7}:H{len(list1) + len(list2) + 7}').Merge()
    ws1.Range('A1').Value = '室内工程报价模型'
    ws1.Range('A1').Font.Bold = True
    ws1.Range(f'A{len(list1) + 4}').Value = '小计'
    ws1.Range(f'A{len(list1) + 5}').Value = '以下材料为选配'
    ws1.Range(f'A{len(list1) + len(list2) + 6}').Value = '小计'
    ws1.Range(f'A{len(list1) + len(list2) + 7}').Value = '合计'
    ws1.Range(f'I{len(list1) + 4}').Value = f'=sum(I4:I{len(list1) + 3})'
    ws1.Range(f'I{len(list1) + len(list2) + 6}').Value = f'=sum(I{len(list1) + 6}:I{len(list1) + len(list2) + 5})'
    ws1.Range(f'I{len(list1) + len(list2) + 7}').Value = f'=I{len(list1) + 4}+I{len(list1) + len(list2) + 6}'
    ws1.Range(f'A1:J{len(list1) + len(list2) + 7}').Interior.ColorIndex = 23
    ws1.Range(f'A3:J{len(list1) + 3}').Interior.ColorIndex = 0
    ws1.Range(f'A{len(list1) + 6}:J{len(list1) + len(list2) + 5}').Interior.ColorIndex = 0
    ws1.Range(f"A1:J{len(list1) + len(list2) + 7}").Borders.LineStyle = 1  # 边框线段
    output_file_path = r'D:/static/media/oderexcel/' + name + '.xlsx'
    wb1.protection.sheet = True
    wb1.protection.password = filenamepassword
    wb1.protection.enable()
    wb1.SaveAs(output_file_path)  # 保存
    wb1.Close(0)  # 关闭

    def close_excel_by_force(excel):  # 关闭进程

        # Get the window's process id's
        hwnd = excel.Hwnd
        import win32process
        t, p = win32process.GetWindowThreadProcessId(hwnd)
        # Ask window nicely to close
        try:
            import win32api
            import win32con
            handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
            if handle:
                win32api.TerminateProcess(handle, 0)
                win32api.CloseHandle(handle)
        except:
            pass

    close_excel_by_force(excel)

测试数据如下

 must=[[1,"单元板","/","/","/","/",5,10,50,""],
          [2,"辅材","/","/","/","/",6,11,66,""],
          [3,"电源","/","/","/","/",7,12,84,""],
           [4,"控制卡","/","/","/","/",8,13,104,""],
           [5,"处理器","/","/","/","/",9,14,126,""],
          [6,"配电箱","/","/","/","/",10,15,150,""],
          [7,"结构","/","/","/","/",11,16,176,""],
          [8,"包边","/","/","/","/",12,17,204,""],
          [9,"安装调试","/","/","/","/",13,18,234,""],]
 uncertain=[[1,"箱体","/","/","/","/",5,50,250,""],
          [2,"多功能卡","/","/","/","/",6,51,306,""],
          [3,"音箱、功放","/","/","/","/",7,52,364,""],
          [4,"电脑","/","/","/","/",8,53,424,""],
          [5,"运输","/","/","/","/",9,54,486,""],
          [6,"施工平台","/","/","/","/",10,55,550,""],
          [7,"综合布线","/","/","/","/",11,56,616,""]]
name="123.xlsx"
filenamepassword='123456'

调用代码如下

p = threading.Thread(target=create_project_excel, args=(must, uncertain, filename, filenamepassword))
p.setDaemon(True)
p.start()
  • 写回答

1条回答 默认 最新

  • 陈年椰子 2022-01-24 10:53
    关注
    wb1.protection.sheet = True 
    

    这个是 openpyxl 的用法吧。

    img

    这样 试试

    
    #  wb1.protection.sheet = True
    #  wb1.protection.password = filenamepassword
    # wb1.protection.enable()
    #  改为
    ws1.Protect(filenamepassword)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月12日
  • 已采纳回答 2月12日
  • 修改了问题 1月22日
  • 修改了问题 1月22日
  • 展开全部

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?