简体   繁体   中英

Displaying an excel table into PyQt gui window

I need bit of assistance with my python code. I am a newbie to python so not very good at it. I have an excel spreadsheet with a bunch of lecture times and I am using the code below;

df = pd.read_excel('/home/pi/timetable1.xlsx')
df['Date'] = pd.to_datetime(df['Date']).dt.strftime("%d-%m-%Y")
now = pd.to_datetime('today').strftime("%d-%m-%Y")
print(df[df['Date'] == now])

which displays a simple line of timetable text such as below [xx:xx is time in 24 hr format];

Lesson 1:  Lesson 2:  Lesson 3:  Lession 4:  Lesson 5:
xx:xx      xx:xx      xx:xx      xx:xx       xx:xx

I have configured it so that the above displayed times only shows the times for the "Current Date". What I am trying to acheive is that I want to use PyQt4 to display this information on a graphical window.

So for example, the below displays "HELLO WORLD" text in the gui window;

def wxfinished():
    attribution3.setText("HELLO WORLD")

attribution3 = QtGui.QLabel(foreGround) 
attribution3.setObjectName("attribution3")
attribution3.setStyleSheet("#attribution3 { " +
                           "background-color: transparent; color: " +
                           Config.textcolor +
                           "; font-size: " +
                           str(int(50 * xscale)) + #50 is the size of the text
                           "px; " +
                           Config.fontattr +
                           "}")
attribution3.setAlignment(Qt.AlignTop)
attribution3.setGeometry(450 * xscale, 670 * yscale, 1000 * xscale, 1000)

w.show()
w.showFullScreen()

sys.exit(app.exec_())

How can I change it so that instead of "HELLO WORLD" I can print out the timetable output?

While I'm not that familiar with Pandas, here's an MRE to help you. In this, a table is made, the labels are set to the excel's labels (lesson x), and the singular row is filled out with the dates.

from PyQt4.QtGui import QApplication, QTableWidget, QLabel, QFont
from PyQt4.QtCore import Qt
import sys
import pandas

CELL_FONT = QFont()
CELL_FONT.setPointSize(8)

def getLessions(): # assuming the code you provided works
    """
    Returns Pandas DataFrame object w/ today's date info
    :return:
    """
    data = pandas.read_excel("path")
    date = pandas.to_datatime(data["Date"]).dtftime("%d-%m-%Y")
    now = pandas.to_datetime('today').strftime("%d-%m-%Y")
    return data[date == now]

def makeWidget():
    datum = getLessions()
    columns = len(datum.columns)

    # Use table to display information
    table = QTableWidget()
    table.setRowCount(1)
    table.setColumnCount(columns)

    table.setHorizontalHeaderLabels(datum.columns) # I think datum.columns returns [str], I could be wrong
    table.setVerticalHeaderLabels([""])

    # Row will be filled with Labels w/ times
    for i in range(columns):
        lession_time = str(datum.iloc[0,i])   # edit: iloc returns a Timestamp object

        label = QLabel()
        label.setFont(CELL_FONT)
        label.setText(lession_time)
        label.setAlignment(Qt.AlignHCenter | Qt.AlignBottom)
        table.setCellWidget(0, i, label)

    return table

def makeWidgetDummy(): # Doesn't use Pandas
    columns = 5

    table = QTableWidget()
    table.setRowCount(1)
    table.setColumnCount(columns)

    horizontal_lbls = ["Lesson {}".format(i + 1) for i in range(columns)]
    table.setHorizontalHeaderLabels(horizontal_lbls)
    table.setVerticalHeaderLabels([""])

    for i in range(columns):
        lession_time = "XX:XX"

        label = QLabel()
        label.setFont(CELL_FONT)
        label.setText(lession_time)
        label.setAlignment(Qt.AlignHCenter | Qt.AlignBottom)
        table.setCellWidget(0, i, label)

def main():
    app = QApplication(sys.argv)
    widget = makeWidget()
    # widget = makeWidgetDummy()
    widget.show()
    app.exec_()

if __name__ == "__main__":
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM