pyqt5输出word,excel,pdf文件(使用office,跟wps的接口)

最近发现了一个挺厉害的人工智能学习网站,内容通俗易懂,风趣幽默,感兴趣的可以点击此链接进行查看:床长人工智能教程

 废话不多说,请看正文!

输出文件的代码

from PyQt5.QAxContainer import QAxWidget
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QFileDialog,QMessageBox
import sys
from PyQt5.QtWidgets import QApplication

class AxWidget(QWidget):

    def __init__(self, *args, **kwargs):
        super(AxWidget, self).__init__(*args, **kwargs)
        self.resize(800, 600)
        layout = QVBoxLayout(self)
        self.axWidget = QAxWidget(self)
        layout.addWidget(self.axWidget)
        layout.addWidget(QPushButton('选择ppt,excel,word,pdf文件',
                                     self, clicked=self.onOpenFile))

    def onOpenFile(self):
        path, _ = QFileDialog.getOpenFileName(
            self, '请选择文件', '', 'excel(*.xlsx *.xls);;word(*.docx *.doc);;pdf(*.pdf)')
        if not path:
            return
        if _.find('*.doc'):
            return self.openOffice(path, 'Word.Application')
        if _.find('*.xls'):
            return self.openOffice(path, 'Excel.Application')
        if _.find('*.pdf'):
            return self.openPdf(path)
        
        
        

    def openOffice(self, path, app):
        self.axWidget.clear()
        if not self.axWidget.setControl(app):
            return QMessageBox.critical(self, '错误', '没有安装  %s' % app)
        self.axWidget.dynamicCall(
            'SetVisible (bool Visible)', 'false')  # 不显示窗体
        self.axWidget.setProperty('DisplayAlerts', False)
        self.axWidget.setControl(path)
        self.axWidget.show()

    def openPdf(self, path):
        self.axWidget.clear()
        if not self.axWidget.setControl('Adobe PDF Reader'):
            return QMessageBox.critical(self, '错误', '没有安装 Adobe PDF Reader')
        #self.axWidget.setControl("{233C1507-6A77-46A4-9443-F871F945D258}")
        self.axWidget.dynamicCall(
            'SetVisible (bool Visible)', 'false')  # 不显示窗体
        self.axWidget.dynamicCall('LoadFile(const QString&)',0,  path)

    
    
    
    def closeEvent(self, event):
        self.axWidget.close()
        self.axWidget.clear()
        self.layout().removeWidget(self.axWidget)
        del self.axWidget
        super(AxWidget, self).closeEvent(event)


if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    w = AxWidget()
    w.show()
    sys.exit(app.exec_())

self.openOffice(path, 'Word.Application')

self.openOffice(path, 'Excel.Application')

Word.Application 是office 中word的接口。 Excel.Application 是excel的接口。

代码运行如下:

 使用wps的接口打开word ,excel.

昨天又试了一下,发现使用wps 的接口打开word,excel 文件更加流畅。

代码如下

from PyQt5.QAxContainer import QAxWidget
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QFileDialog,QMessageBox


class AxWidget(QWidget):

    def __init__(self, *args, **kwargs):
        super(AxWidget, self).__init__(*args, **kwargs)
        self.resize(800, 600)
        layout = QVBoxLayout(self)
        self.axWidget = QAxWidget(self)
        layout.addWidget(self.axWidget)
        layout.addWidget(QPushButton('选择excel,word,pdf文件',
                                     self, clicked=self.onOpenFile))

    def onOpenFile(self):
        path, _ = QFileDialog.getOpenFileName(
            self, '请选择文件', '', 'excel(*.xlsx *.xls);;word(*.docx *.doc);;pdf(*.pdf)')
        if not path:
            return
        if _.find('*.doc'):
            return self.openOffice(path, 'kwps.Application')  #word 的接口
        if _.find('*.xls'):
            return self.openOffice(path, 'ket.Application')   #excel的接口
        
        if _.find('*.pdf'):
            return self.openPdf(path)
        
        
        

    def openOffice(self, path, app):
        self.axWidget.clear()
        if not self.axWidget.setControl(app):
            return QMessageBox.critical(self, '错误', '没有安装  %s' % app)
        self.axWidget.dynamicCall(
            'SetVisible (bool Visible)', 'false')  # 不显示窗体
        self.axWidget.setProperty('DisplayAlerts', False)
        self.axWidget.setControl(path)
        self.axWidget.show()

    def openPdf(self, path):
        self.axWidget.clear()
        if not self.axWidget.setControl('Adobe PDF Reader'):
            return QMessageBox.critical(self, '错误', '没有安装 Adobe PDF Reader')
        #self.axWidget.setControl("{233C1507-6A77-46A4-9443-F871F945D258}")
        self.axWidget.dynamicCall(
            'SetVisible (bool Visible)', 'false')  # 不显示窗体
        self.axWidget.dynamicCall('LoadFile(const QString&)',0,  path)

    
    
    
    def closeEvent(self, event):
        self.axWidget.close()
        self.axWidget.clear()
        self.layout().removeWidget(self.axWidget)
        del self.axWidget
        super(AxWidget, self).closeEvent(event)


if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    w = AxWidget()
    w.show()
    sys.exit(app.exec_())

 self.openOffice(path, 'kwps.Application')  #word 的接口

Kwps.Application 是wps 中的word文档的接口,Ket.Application 是excel文件的接口。

  • 12
    点赞
  • 128
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 33
    评论
PyQt5是一个用于创建图形用户界面(GUI)的Python库。要处理上传的Excel文件,首先需要使用PyQt5创建一个GUI窗口,用于接收文件上传。 下面是一个简单的示例代码: ```python import sys from PyQt5.QtWidgets import QApplication, QFileDialog, QMainWindow from pandas import read_excel class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("Excel文件处理") self.setGeometry(100, 100, 300, 200) self.show() def openFile(self): options = QFileDialog.Options() options |= QFileDialog.ReadOnly fileName, _ = QFileDialog.getOpenFileName(self, "选择Excel文件", "", "Excel文件 (*.xlsx *.xls)", options=options) if fileName: df = read_excel(fileName) # 使用pandas读取Excel文件 print(df) # 处理Excel文件的操作 if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.openFile() sys.exit(app.exec_()) ``` 在上述代码中,我们创建了一个MainWindow类继承自QMainWindow,并重写了initUI方法用于初始化窗口。在openFile方法中,我们使用QFileDialog打开文件对话框,以选择上传的Excel文件。 选择Excel文件后,我们使用pandas库的read_excel函数来读取该文件,并将其存储在df变量中。你可以根据需要对Excel文件进行处理,例如打印或进行其他操作。 这只是一个基本的示例,你可以根据自己的需求扩展该代码。希望对你有所帮助!
评论 33
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zpeien

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值