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
|
def export_planning(self):
fname = QtWidgets.QFileDialog.getSaveFileName(self, 'Enregistrer un fichier', '', '*.xlsx')
if fname[0] != '':
myTableView = self.tableWidget_4
rows = myTableView.rowCount()
columns = myTableView.columnCount()
totalWidth = 0
totalHeight = 0
for c in range(columns):
if c == 0:
column_to_fixed = myTableView.columnWidth(c)
totalWidth += myTableView.columnWidth(c)
for r in range(rows):
totalHeight += myTableView.rowHeight(r)
printer = QtGui.QPixmap(totalWidth, totalHeight)
painter = QtGui.QPainter(printer)
area = printer.rect()
xscale = area.width() / totalWidth
yscale = area.height() / totalHeight
painter.scale(xscale, yscale)
painter.begin(printer)
for r in range(rows):
for c in range(columns):
if r == 0:
if c == 0:
idx = myTableView.model().index(r, c)
else:
if c % 7 == 1:
idx = myTableView.model().index(r, c)
else:
idx = myTableView.model().index(r, c)
option = myTableView.viewOptions()
option.rect = myTableView.visualRect(idx)
if r % 2 == 1 and c == 0:
brush = QtGui.QBrush(QtGui.QColor(220, 220, 220), QtCore.Qt.SolidPattern)
painter.fillRect(option.rect, brush)
else:
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255), QtCore.Qt.SolidPattern)
painter.fillRect(option.rect, brush)
myTableView.itemDelegate().paint(painter, option, idx)
painter.end()
printer.save("temp.png", "PNG")
workbook = Workbook(fname[0])
worksheet = workbook.add_worksheet('Planning')
worksheet.protect()
worksheet.freeze_panes(0, int(int(column_to_fixed/8.43)/8.43))
worksheet.insert_image('A2', './temp.png')
workbook.close()
os.remove('./temp.png') |
Partager