Answer the question
In order to leave comments, you need to log in
Python adding to combobox?
how to add sheet names to ComboBox and then get them in sheet_by_index[]
import xlrd, csv
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class OpenFile(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setGeometry(300, 300, 325, 100)
self.setWindowTitle(self.trUtf8('Выбор файла'))
self.setWindowIcon(QtGui.QIcon('imageformats\icon.jpeg'))
pButton = QtGui.QPushButton('Открыть файл', self)
pButton.setGeometry(25, 15, 125, 25)
self.connect(pButton, QtCore.SIGNAL('clicked()'), self.showDialog)
quit = QtGui.QPushButton('Выход', self)
quit.setGeometry(175, 15, 125, 25)
self.connect(quit, QtCore.SIGNAL('clicked()'),
QtGui.qApp, QtCore.SLOT('quit()'))
comboBox = QtGui.QComboBox(self)
comboBox.setGeometry(QtCore.QRect(25, 55, 125, 25))
comboBox.setObjectName(self.trUtf8("comboBox"))
def showDialog(self):
filename = QtGui.QFileDialog.getOpenFileName(self, 'Выбор файла', '*xls')
wbr = xlrd.open_workbook(filename,formatting_info=True)
sheetName = []
for sName in wbr.sheet_names():
sheetName.append(wbr.sheet_by_name(sName))
#//////////////////Индекс листа///////////////////////
#sheet = wbr.sheet_by_index(0)
vals = []
i = 0
a = 0
for rows in range(sheet.nrows):
vals.append(sheet.row_values(rows))
#/////////////Удаляем из массива шапку Промоактивности//////////////
for a in range(4):
del vals[0]
a = +1
file_csv = open('C:\promo.csv', 'w')
for i in range(sheet.nrows - 4):
stroka_0 = str(vals[i]).replace('[','')
stroka_1 = stroka_0.replace(',',';')
stroka_2 = stroka_1.replace(']','')
stroka_3 = stroka_2.replace(' ','')
stroka_4 = stroka_3.replace("'",'')
stroka_5 = stroka_4.replace(".0",'')
file_csv.write(stroka_5 + "\n")
i=+1
file_csv.close()
app = QtGui.QApplication(sys.argv)
cd = OpenFile()
cd.show()
sys.exit(app.exec())
Answer the question
In order to leave comments, you need to log in
blah blah blah create comboBox as a variable of the created OpenFile object:
class OpenFile(QtGui.QMainWindow):
def __init__(self, parent=None):
... ... ...
self.comboBox = QtGui.QComboBox(self)
self.comboBox.setGeometry(QtCore.QRect(25, 55, 125, 25))
self.comboBox.setObjectName(self.trUtf8("comboBox"))
for sName in wbr.sheet_names():
sheetName.append(wbr.sheet_by_name(sName))
self.comboBox.addItem(str(sName))
class OpenFile(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
... ... ...
pButton2 = QtGui.QPushButton('записать в csv', self)
pButton2.setGeometry(175, 55, 125, 25)
self.connect(pButton2, QtCore.SIGNAL('clicked()'), self.slot_list_selected)
def showDialog(self):
filename = QtGui.QFileDialog.getOpenFileName(self, 'Выбор файла', '*xls')
self.wbr = xlrd.open_workbook(filename, formatting_info=True)
self.comboBox.addItems(self.wbr.sheet_names())
def slot_list_selected(self):
# //////////////////Индекс листа///////////////////////
sheet = self.wbr.sheet_by_index(self.comboBox.currentIndex())
... ... ...
class OpenFile(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setGeometry(300, 300, 325, 100)
self.setWindowTitle(self.trUtf8('Выбор файла'))
self.setWindowIcon(QtGui.QIcon('imageformats\icon.jpeg'))
pButton = QtGui.QPushButton('Открыть файл', self)
pButton.setGeometry(25, 15, 125, 25)
self.connect(pButton, QtCore.SIGNAL('clicked()'), self.showDialog)
quit = QtGui.QPushButton('Выход', self)
quit.setGeometry(175, 15, 125, 25)
self.connect(quit, QtCore.SIGNAL('clicked()'),
QtGui.qApp, QtCore.SLOT('quit()'))
self.comboBox = QtGui.QComboBox(self)
self.comboBox.setGeometry(QtCore.QRect(25, 55, 125, 25))
self.comboBox.setObjectName(self.trUtf8("comboBox"))
self.comboBox.activated.connect(self.slot_list_selected)
def showDialog(self):
filename = QtGui.QFileDialog.getOpenFileName(self, 'Выбор файла', '*xls')
self.wbr = xlrd.open_workbook(filename, formatting_info=True)
self.comboBox.blockSignals(True)
self.comboBox.clear()
self.comboBox.addItems(["выберите лист"] + self.wbr.sheet_names())
self.comboBox.blockSignals(False)
def slot_list_selected(self, list_index):
if list_index:
# //////////////////Индекс листа///////////////////////
sheet = self.wbr.sheet_by_index(list_index-1)
print(list_index)
vals = []
i = 0
a = 0
for rows in range(sheet.nrows):
vals.append(sheet.row_values(rows))
# /////////////Удаляем из массива шапку Промоактивности//////////////
for a in range(4):
del vals[0]
a = +1
file_csv = open('C:\promo.csv', 'w')
for i in range(sheet.nrows - 4):
stroka_0 = str(vals[i]).replace('[', '')
stroka_1 = stroka_0.replace(',', ';')
stroka_2 = stroka_1.replace(']', '')
stroka_3 = stroka_2.replace(' ', '')
stroka_4 = stroka_3.replace("'", '')
stroka_5 = stroka_4.replace(".0", '')
file_csv.write(stroka_5 + "\n")
i = +1
file_csv.close()
I choose a book first. In ComboBox I load sheets from the book. I select a sheet and write the data from it into a csv file! You can ask a question if it’s not difficult for you to describe as beautifully as “That is, all the code below most likely needs to be done as a separate method” how to do it!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question