栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

记录python开发第一个app

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

记录python开发第一个app

记录python开发第一个app
  • word转HTML功能
  • Pyqt5图像化UI
  • 打包app
    • 1.安装py2app
    • 2.创建setup.py文件
    • 3.解决报错py2applet:command not found
    • 4.打包

word转HTML功能
    #转化HTML
    def createHTML(self):
        
        html = PyDocX.to_html(str(self.docx_path))

        # 过滤掉不支持的标签(安卓暂不支持style、script等) #
        html_remove = re.sub('','',html)

        # 样式兼容 #
        html_style = BeautifulSoup(html_remove,'html.parser')
        # body边距及对齐方式
        html_style.find('body')['style'] = 'padding: 50px 30px;text-align: justify;'
        # 居中
        text_center =  html_style.select('.pydocx-center')
        for item in text_center:
            item['style'] = 'display:block;text-align:center;padding:20px 0px'
        # 局右
        text_right =  html_style.select('.pydocx-right')
        for item in text_right:
            item['style'] = 'display:block;text-align:right;'
        # 下划线
        text_underline = html_style.select('.pydocx-underline')
        for item in text_underline:
            item['style'] = 'text-decoration:underline;'

        # 创建HTML #
        html_path = 'index.html'
        f = open(html_path,'w',encoding='utf-8')
        f.write(str(html_style))
        f.close()
        # 转化完成路径
        html_path1 = os.path.abspath('index.html')
        self.lb2.adjustSize()
        self.lb2.setGeometry(QtCore.QRect(20, 140, 360, 100))
        self.lb2.setWordWrap(True)
        self.lb2.setAlignment(QtCore.Qt.AlignTop)
        self.lb2.setText('转化完成路径:'+html_path1)
Pyqt5图像化UI
    #初始化UI
    def initUI(self):
        self.setGeometry(200,200,400,200)
        self.setWindowTitle('word转html')

        self.bt1 = QPushButton('选择文件',self)
        self.bt1.move(10,20)

        self.lb1 = QLabel('上传文件路径:',self)
        self.lb1.move(20,60)
        self.lb1.setObjectName("lb1")
        
        self.bt2 = QPushButton('立即转换',self)
        self.bt2.move(10,100)

        self.lb2 = QLabel('转化完成路径:',self)
        self.lb2.move(20,140)
        self.lb2.setObjectName("lb2")

        self.show()

        self.bt1.clicked.connect(self.showFile)
        self.bt2.clicked.connect(self.createHTML)
    #选择文件
    def showFile(self):
        file = QFileDialog.getOpenFileName(None,  "选取文件","./", "All Files (*);;Text Files (*.txt)")[0] 
        self.lb1.adjustSize()
        self.lb1.setGeometry(QtCore.QRect(20, 60, 360, 100))
        self.lb1.setWordWrap(True)
        self.lb1.setAlignment(QtCore.Qt.AlignTop)
        self.lb1.setText('上传文件路径:'+file)

        self.docx_path = file
打包app

开发其实没什么难度,因为本身python支持的库很多,这里用的打包工具是py2app

1.安装py2app
pip install py2app
2.创建setup.py文件

首先进入工程目录:cd 项目路径

py2applet --macke-setup demo.py#自己的打包文件

注:报错:py2applet:command not found

3.解决报错py2applet:command not found
#首先查找py2applet路径
sudo find / -name "py2applet" -type f 
#本机路径为
/System/Volumes/Data/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/bin/py2applet
#继续生成setup.py
/System/Volumes/Data/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/bin/py2applet --macke-setup demo.py
4.打包
python3 setup.py py2app --site-packages

提示这一坨的时候,恭喜你打包完成!!!

下边是对应的包及演示:


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009566.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号