Python Forum
[PyQt] How to Copy-Paste a table from Office apps to QTableView?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] How to Copy-Paste a table from Office apps to QTableView?
#1
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 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
Question
Could you help me with the right solution of this problem? Smile
Reply


Messages In This Thread
How to Copy-Paste a table from Office apps to QTableView? - by Vittorio - Aug-04-2021, 08:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt5 QTableView SQLite : edit cell HeinKurz 2 2,417 Mar-27-2023, 10:41 AM
Last Post: HeinKurz
  [PyQt] QStyledItemDelegate and QTableView malonn 1 1,654 Feb-07-2023, 07:15 PM
Last Post: malonn
  [PyQt] QTableView set header labels HeinKurz 2 6,845 Jan-23-2023, 08:46 AM
Last Post: HeinKurz
  [PyQt] Determine whether text in QTableView cell is fully visible or not random_nick 0 993 Oct-27-2022, 09:29 PM
Last Post: random_nick
  [PyQt] QTableView: scroll to top cell of the screen random_nick 2 2,862 Oct-08-2022, 12:29 AM
Last Post: random_nick
  [PyQt] [Solved]Add a Blank Row To QTableView Extra 3 5,577 Oct-02-2022, 04:53 PM
Last Post: Extra
  How to update the list of a combo box in a QTableView panoss 10 6,269 Feb-05-2022, 03:24 PM
Last Post: panoss
  [Tkinter] Override the paste function(copy from excel file - paste in separate tkinter entryes) AndreiV 3 4,670 Jun-05-2020, 04:46 PM
Last Post: AndreiV
  [PyQt] Qtableview adapte size to WBPYTHON 3 11,340 Mar-23-2020, 01:51 AM
Last Post: deanhystad
  [PyGUI] Showing text in QTableView sequence 0 3,082 Jan-20-2019, 05:00 PM
Last Post: sequence

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020