美文网首页
PYQT5(15)导出为表格

PYQT5(15)导出为表格

作者: 弗兰克万岁 | 来源:发表于2020-10-11 19:54 被阅读0次
            def savefile(self):
            try:
                import os
                if os.path.exists('last_save_path.txt'):
                    path=self.load_last_path()
                else:
                    path='/'
                from PyQt5.QtWidgets import QDialog
                file = QDialog()
                from PyQt5.QtWidgets import QFileDialog
                fname = QFileDialog.getExistingDirectory(file, 'open', path)
                self.save_last_path(fname)
                filename =fname+'/任务表.xls'
                import xlsxwriter
                self.wbk = xlsxwriter.Workbook(filename)
                self.sheet = self.wbk.add_worksheet("sheet")
                self.add2()
                self.wbk.close()
    
                self.msg1('成功生成任务表,在文件目录下可以找到')
            except Exception as e:
                print(e)
                self.msg1('生成任务表失败')
    
        def add2(self):
            row = 0#窗口行,从第几行开始,下面循环中还有一个
            col = 0#窗口列
            start_index=2
            company_name = str('afsadfasdfasf')
            company_tel='''Pfsgf
                             '''
            merge_format = self.wbk.add_format({
                'bold': True,
                'border': 8,
                'align': 'center',  # 水平居中
                'valign': 'vcenter',  # 垂直居中
            })
            self.sheet.merge_range(0,0,0,11,company_name,merge_format)
            self.sheet.merge_range(1, 0, 1, 11, company_tel,merge_format)
            for i in range(self.table_my.columnCount()):#列
                for x in range(start_index,self.table_my.rowCount()+start_index):#行
                    if x==start_index:#第一列,标题栏
                        self.sheet.set_row(x, 30.0)#设置行高
                        self.sheet.set_column(i, self.table_my.columnCount(), 15)#设置列宽
                    # elif i in self.price_out:#屏蔽
                    #     continue
    
                    else:#不是第一行,不是第一列
                        # self.sheet.set_row(x, 180.0)#行高
                        self.sheet.set_column(i, self.table_my.columnCount(), 15)#列宽
                    try:
    
                        teext = str(self.table_my.item(row, col).text())
                        self.sheet.write(x, i, teext)#写入表格
                        row += 1
                    except AttributeError:
                        row += 1
                row = 0
                col += 1
                self.sheet.set_row(0, 30)
                self.sheet.set_row(1, 30)
        def save_last_path(self,url):
            try:
                with open('last_save_path.txt','w') as f:
                    f.write(url)
                    return 1
            except Exception as e:
                print(e)
        def load_last_path(self):
            #报表存储位置
            with open('last_save_path.txt','r') as f:
                res=f.read()
            return res
    

    相关文章

      网友评论

          本文标题:PYQT5(15)导出为表格

          本文链接:https://www.haomeiwen.com/subject/ikzepktx.html