Pythonimport sysfrom PyQt4.QtCore import *from PyQt4.QtGui import *from functools import partialimport xlwtimport xlrd class My_Widget(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.table = QTableWidget(self) self.table.setRowCount(20) self.table.setColumnCount(20) self.table.setGeometry(0, 29, 1600, 1600) self.table.show() self.table.horizontalHeader().hide() self.table.verticalHeader().hide() self.table.setColumnWidth(0, 30) self.table.setRowHeight(0, 30) self.table.resizeColumnsToContents() delegate = MyDelegate() self.table.setItemDelegate(delegate) #Цвет таблицы и стиль self.table.setStyleSheet("gridline-color: #FFC0CB") self.table.setGridStyle(1) self.table.setFrameStyle(QFrame.NoFrame) #--------------------------------------------- self.setGeometry(300, 300, 800, 600) self.setWindowTitle('icon')#--------------------------------------------- self.lay = QHBoxLayout() self.lay.setAlignment(Qt.AlignLeft | Qt.AlignTop) self.lay.setMargin(0) self.lay.setSpacing(0) self.setLayout(self.lay) self.BUTTNS = [ {'text': u'', 'img': 'sym1.png', 'ico': 'sym1.png'}, {'text': u'', 'img': 'sym2.png', 'ico': 'sym2.png'}]#--------------------------------------------- for i in self.BUTTNS: i["btn"] = QPushButton(i["text"]) i["btn"].setIcon(QIcon(i["ico"])) i["btn"].clicked.connect(partial(self.on_clicked_btn, QPixmap(i["img"]))) self.lay.addWidget(i["btn"])#--------------------------------------------- self.btn = QPushButton("save xls", self) self.btn.setGeometry(75, 0, 80, 30) self.btn.sizeHint() self.connect(self.btn, SIGNAL("clicked()"), self.save_file) self.btn2 = QPushButton("open xls", self) self.btn2.setGeometry(150, 0, 80, 30) self.btn2.sizeHint() self.connect(self.btn2, SIGNAL("clicked()"), self.open_file)#--------------------------------------------- def on_clicked_btn(self, img): item = QTableWidgetItem() item.setData(Qt.DecorationRole, img) self.table.setItem(self.table.currentRow(), self.table.currentColumn(), item) #--------------------------------------------- #-------------------------------------------- def save_file(self): filename = (QFileDialog.getSaveFileName(self, 'Сохранить', '', ".xls(*.xls)")) wbk = xlwt.Workbook() sheet = wbk.add_sheet("схема", cell_overwrite_ok=True) self.s_f(sheet) wbk.save(filename) def s_f(self, sheet): for currentColumn in range(self.table.columnCount()): for currentRow in range(self.table.rowCount()): my_icon = self.table.item(currentRow, currentColumn) sheet.write(currentRow, currentColumn, str(my_icon or '')) #------------------------------------------- def open_file(self): filename = QFileDialog.getOpenFileName(self, 'Открыть', '', '.xls(*.xls)') book = xlrd.open_workbook(filename) sheet = book.sheet_by_index(0) data = [[sheet.cell_value(r, c)for c in range(sheet.ncols)]for r in range(sheet.nrows)] for row, columnvalues in enumerate(data): for column, value in enumerate(columnvalues): item = QTableWidgetItem() item.setData(Qt.DisplayRole, str(value)) self.table.setItem(row, column, item) #--------------------Делегат для ячейкиclass MyDelegate(QStyledItemDelegate): def paint(self, painter, option, index): img = index.model().data(index, Qt.DecorationRole) if img is None: super().paint(painter, option, index) return rect = option.rect w, h = rect.size().width(), rect.size().height() img = img.scaled(w, h, Qt.KeepAspectRatio, Qt.SmoothTransformation) painter.drawPixmap(rect, img) item_option = QStyleOptionViewItem(option) self.initStyleOption(item_option, index) #Для полупрозрачности выделения if item_option.state & QStyle.State_Selected: color = item_option.palette.color(QPalette.Highlight) color.setAlpha(180) painter.save() painter.setPen(Qt.NoPen) painter.setBrush(color) painter.drawRect(rect) painter.restore() # super().paint(painter, option, index) #--------------if __name__ == '__main__': app = QApplication(sys.argv) widget = My_Widget() widget.show() sys.exit(app.exec_())
Pythonsheet.insert_bitmap("sym1.bmp",1,1)
Pythonsheet.insert_bitmap(currentRow, currentColumn, my_icon)
Pythonicon_file_name = "cell_icon.bmp" ## Тут начало цикла для перебора ячеек# item = self.table.item(currentRow, currentColumn) # Сохранение иконки ячейки в файлimg = item.data(Qt.DecorationRole)img.save(icon_file_name) sheet.insert_bitmap(icon_file_name, currentRow, currentColumn) ## Тут конец цикла для перебора ячеек# # Теперь иконку можно удалитьimport osif os.path.exist(icon_file_name): os.remove(icon_file_name)
Python def save_file(self): filename = (QFileDialog.getSaveFileName(self, 'Сохранить', '', ".xls(*.xls)")) wbk = xlwt.Workbook() sheet = wbk.add_sheet("схема", cell_overwrite_ok=True) for currentColumn in range(self.table.columnCount()): for currentRow in range(self.table.rowCount()): my_icon = self.table.item(currentRow, currentColumn) sheet.insert_bitmap(my_icon, currentRow, currentColumn) wbk.save(filename)
Pythonitem = self.table.item(currentRow, currentColumn) # Сохранение иконки ячейки в файлimg = item.data(Qt.DecorationRole)img.save(icon_file_name) sheet.insert_bitmap(icon_file_name, currentRow, currentColumn)
Pythonmy_icon = self.table.item(currentRow, currentColumn)sheet.insert_bitmap(my_icon, currentRow, currentColumn)