Pasting python's tabulate output to Microsoft Office editors

早过忘川 提交于 2021-01-28 08:30:34

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!