当我尝试浏览 .xlsx 文件时,会打开一个新的 Excel 文档
When I try to browse for a .xslx file a new Excel document opens
我目前正在使用 PyQt5 开发 GUI。
我正在使用菜单栏加载文件,当我单击它时,程序会调用此函数:
def getxlsbase(self):
filePath_base, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Select file', './', 'Excel Files (*.xls *.xlsx)')
base = xlrd.open_workbook(filePath_base)
hoja_base = base.sheet_by_index(0)
num_row = hoja_base.nrows
num_col = hoja_base.ncols
当浏览器 window 打开并且我 select 文件时,一个空白的 Excel 文档打开,我该如何避免这种情况?
编辑:
我试过这个方法,但我仍然有这个问题,它只发生在 excel 文档中。我正在使用 Spyder3
from tkinter import *
from tkinter import filedialog
root = Tk()
root.withdraw()
filepath = filedialog.askopenfilename()
print(filepath)
您提供的方法正确。
import sys
import xlrd
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QAxContainer import QAxWidget
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('Select file', self, clicked=self.getxlsbase))
def getxlsbase(self):
filePath_base, _ = QFileDialog.getOpenFileName(self,
'Select file',
'./',
'Excel Files (*.xls *.xlsx)')
print("filePath_base->", filePath_base)
base = xlrd.open_workbook(filePath_base)
print("base->", base)
hoja_base = base.sheet_by_index(0)
print("hoja_base->", hoja_base)
num_row = hoja_base.nrows
print("num_row->", num_row)
num_col = hoja_base.ncols
print("num_col->", num_col)
return self.openOffice(filePath_base, 'Excel.Application')
def openOffice(self, path, app):
self.axWidget.clear()
if not self.axWidget.setControl(app):
return QMessageBox.critical(self, 'Error', 'No installation %s' % app)
self.axWidget.dynamicCall(
'SetVisible (bool Visible)', 'false')
self.axWidget.setProperty('DisplayAlerts', False)
self.axWidget.setControl(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)
ex = AxWidget()
ex.show()
sys.exit(app.exec_())
我不知道我是否理解正确的问题,但是如果像我一样,当您打开一个 excel 文档时,一个 window 打开并带有一个文件选择,那么您可以这样做这个:
def get_path_excel_file(self):
excel_path, _ = QFileDialog.getOpenFileName(self,
'Select file',
'./',
'Excel Files (*.xls *.xlsx)')
# assign a path to some element
self.label_5.setText(f"{excel_path}")
# we get the path from this element
book = openpyxl.load_workbook(fr"{self.label_5.text()}")
我目前正在使用 PyQt5 开发 GUI。 我正在使用菜单栏加载文件,当我单击它时,程序会调用此函数:
def getxlsbase(self):
filePath_base, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Select file', './', 'Excel Files (*.xls *.xlsx)')
base = xlrd.open_workbook(filePath_base)
hoja_base = base.sheet_by_index(0)
num_row = hoja_base.nrows
num_col = hoja_base.ncols
当浏览器 window 打开并且我 select 文件时,一个空白的 Excel 文档打开,我该如何避免这种情况?
编辑: 我试过这个方法,但我仍然有这个问题,它只发生在 excel 文档中。我正在使用 Spyder3
from tkinter import *
from tkinter import filedialog
root = Tk()
root.withdraw()
filepath = filedialog.askopenfilename()
print(filepath)
您提供的方法正确。
import sys
import xlrd
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QAxContainer import QAxWidget
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('Select file', self, clicked=self.getxlsbase))
def getxlsbase(self):
filePath_base, _ = QFileDialog.getOpenFileName(self,
'Select file',
'./',
'Excel Files (*.xls *.xlsx)')
print("filePath_base->", filePath_base)
base = xlrd.open_workbook(filePath_base)
print("base->", base)
hoja_base = base.sheet_by_index(0)
print("hoja_base->", hoja_base)
num_row = hoja_base.nrows
print("num_row->", num_row)
num_col = hoja_base.ncols
print("num_col->", num_col)
return self.openOffice(filePath_base, 'Excel.Application')
def openOffice(self, path, app):
self.axWidget.clear()
if not self.axWidget.setControl(app):
return QMessageBox.critical(self, 'Error', 'No installation %s' % app)
self.axWidget.dynamicCall(
'SetVisible (bool Visible)', 'false')
self.axWidget.setProperty('DisplayAlerts', False)
self.axWidget.setControl(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)
ex = AxWidget()
ex.show()
sys.exit(app.exec_())
我不知道我是否理解正确的问题,但是如果像我一样,当您打开一个 excel 文档时,一个 window 打开并带有一个文件选择,那么您可以这样做这个:
def get_path_excel_file(self):
excel_path, _ = QFileDialog.getOpenFileName(self,
'Select file',
'./',
'Excel Files (*.xls *.xlsx)')
# assign a path to some element
self.label_5.setText(f"{excel_path}")
# we get the path from this element
book = openpyxl.load_workbook(fr"{self.label_5.text()}")