问题
Quite frequently I need to copy-paste small tables from an SQL editor into Microsoft Office programs (Outlook and OneNote) and I want it to look nice as I paste it. So I wrote a short script taking the data from the clipboard, processing it with Tabulate and returning it to the clipboard.
This works very well when I paste the new table into Notepad++ and other editors. It completely messes up when I paste into Outlook.
If I paste into Notepad++ and then copy paste from there, everything's fine.
I tried the different table formats, and I tried playing around with Outlook's editor options.
Would really appreciate any insights!
Thanks!
See code:
import win32clipboard
import pandas as pd
from tabulate import tabulate
df = pd.read_clipboard()
head = df.columns.tolist()
val = df.values
table = tabulate(val,headers=head,tablefmt="grid")
# set clipboard data
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(str(table))
win32clipboard.CloseClipboard()
来源:https://stackoverflow.com/questions/41356944/pasting-pythons-tabulate-output-to-microsoft-office-editors