Aug-04-2021, 08:52 AM
Hi there,
I’m new in Python 3 & PyQt5 and I was faced with a problem that stunned me with its ruthlessness. I assume that my inexperience in the Python is the root cause but nevertheless…
Task
The task is to copy table from office applications (MS Office, LibreOffice) and to paste it into GUI that was implemented by PyQt5 and QTableView. I studied some elementary examples on the internet, but my task is a little bit more complicated. It's needed to copy square made of some values that are surrounded by empty cells on all sides:
Actually, this selection should be copied together with both numbers and empty cells.
What has been tried
Ctrl+C Ctrl+V don’t work, so I tried more complicated ways listed below
QtWidget.QApplication.clipboard().text()
I started with the simplest - text content of
Getting the html-content of the clipboard, in principle, solved the problem, but in a very clumsy way. HTML tagged text had to be carefully cleaned from any garbage tags, which Word produced especially much. Long, complicated, no certainty that with office program upgrades I will not have to rewrite the algorithm.
pandas.read_clipboard(header=None)
I thought that pandas could help me, but found that output of
Could you help me with the right solution of this problem?
I’m new in Python 3 & PyQt5 and I was faced with a problem that stunned me with its ruthlessness. I assume that my inexperience in the Python is the root cause but nevertheless…
Task
The task is to copy table from office applications (MS Office, LibreOffice) and to paste it into GUI that was implemented by PyQt5 and QTableView. I studied some elementary examples on the internet, but my task is a little bit more complicated. It's needed to copy square made of some values that are surrounded by empty cells on all sides:
Actually, this selection should be copied together with both numbers and empty cells.
What has been tried
Ctrl+C Ctrl+V don’t work, so I tried more complicated ways listed below
QtWidget.QApplication.clipboard().text()
I started with the simplest - text content of
QtWidget.QApplication.clipboard()
and found a complete mayhem that depends on the source of a table:Output: MS Office Excel: '\t\t\n\t1\t2\n\t3\t4\n'
LibreOffice Calc: '1\t2\n3\t4\n'
MS Office Word: '\t\t\t\n\t1\t2\t\n\t3\t4\t\n\t\t\t\n'
LibreOffice Writer: '\n\n\n\n\n1\n2\n\n\n3\n4\n\n\n\n\n\n'
QtWidget.QApplication.clipboard().text("html")Getting the html-content of the clipboard, in principle, solved the problem, but in a very clumsy way. HTML tagged text had to be carefully cleaned from any garbage tags, which Word produced especially much. Long, complicated, no certainty that with office program upgrades I will not have to rewrite the algorithm.
pandas.read_clipboard(header=None)
I thought that pandas could help me, but found that output of
pandas.read_clipboard(header=None)
is as absurd as QtWidget.QApplication.clipboard().text():
output:Output:MS Excel:
2
0 1
NaN NaN NaN
1.0 2.0
3.0 4.0
LibreOffice Calc:
0 1
0 1 2
1 3 4
MS Word:
3
0 1 2
NaN NaN NaN NaN
1.0 2.0 NaN
3.0 4.0 NaN
NaN NaN NaN
LibreOffice Writer:
0
0 1
1 2
2 3
3 4
QuestionCould you help me with the right solution of this problem?