forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewOffice.py
71 lines (59 loc) · 2.22 KB
/
ViewOffice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2017年4月6日
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: 892768447@qq.com
@file: ViewOffice
@description:
"""
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, '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)
def openPdf(self, path):
self.axWidget.clear()
if not self.axWidget.setControl('Adobe PDF Reader'):
return QMessageBox.critical(self, '错误', '没有安装 Adobe PDF Reader')
self.axWidget.dynamicCall('LoadFile(const QString&)', 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_())