当我尝试浏览.xslx文件时,将打开一个新的Excel文档。

时间:2018-09-18 17:28:20

标签: python tkinter pyqt5 xlrd

我目前正在使用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

打开浏览窗口并选择文件后,将打开一个空白Excel文档,如何避免这种情况?

编辑: 我尝试了这种方法,但仍然存在此问题,它仅在excel文档中发生。我正在使用Spyder3

from tkinter import *
from tkinter import filedialog

root = Tk()
root.withdraw()
filepath = filedialog.askopenfilename()
print(filepath)

2 个答案:

答案 0 :(得分:1)

我不知道我是否正确理解了这个问题,但是如果像我一样,当您打开一个 excel 文档时,会打开一个带有文件选择的窗口,那么您可以这样做:

    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()}")

答案 1 :(得分:0)

您提供的方法可以正常工作。

import Vue from 'vue'
import Vuetify from 'vuetify'
import App from './App'
import router from './router'
import 'vuetify/dist/vuetify.min.css'
import firebase from "firebase";
import VueFire from 'vuefire'
import "firebase/firestore";

Vue.config.productionTip = false

Vue.use(Vuetify)
Vue.use(VueFire)

var config = {
    boiler plate initialization copied from Firebase console
 };

firebase.initializeApp(config);

export const db = firebase.firestore();
const settings = { timestampsInSnapshots: true };
db.settings(settings);

/* eslint-disable no-new */
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
  new Vue({
   el: '#app',
   router,
   render: h => h(App)
 })
 unsubscribe() 
})

enter image description here