Attempt to write text representation of object to the system clipboard The clipboard can be then pasted into Excel for example. Parameters ---------- obj : the object to write to the clipboard excel : bool, defaults to True if True, use the provided separator, w
(
obj, excel: bool | None = True, sep: str | None = None, **kwargs
)
| 133 | |
| 134 | |
| 135 | def to_clipboard( |
| 136 | obj, excel: bool | None = True, sep: str | None = None, **kwargs |
| 137 | ) -> None: # pragma: no cover |
| 138 | """ |
| 139 | Attempt to write text representation of object to the system clipboard |
| 140 | The clipboard can be then pasted into Excel for example. |
| 141 | |
| 142 | Parameters |
| 143 | ---------- |
| 144 | obj : the object to write to the clipboard |
| 145 | excel : bool, defaults to True |
| 146 | if True, use the provided separator, writing in a csv |
| 147 | format for allowing easy pasting into excel. |
| 148 | if False, write a string representation of the object |
| 149 | to the clipboard |
| 150 | sep : optional, defaults to tab |
| 151 | other keywords are passed to to_csv |
| 152 | |
| 153 | Notes |
| 154 | ----- |
| 155 | Requirements for your platform |
| 156 | - Linux: xclip, or xsel (with PyQt4 modules) |
| 157 | - Windows: |
| 158 | - OS X: |
| 159 | """ |
| 160 | encoding = kwargs.pop("encoding", "utf-8") |
| 161 | |
| 162 | # testing if an invalid encoding is passed to clipboard |
| 163 | if encoding is not None and encoding.lower().replace("-", "") != "utf8": |
| 164 | raise ValueError("clipboard only supports utf-8 encoding") |
| 165 | |
| 166 | from pandas.io.clipboard import clipboard_set |
| 167 | |
| 168 | if excel is None: |
| 169 | excel = True |
| 170 | |
| 171 | if excel: |
| 172 | try: |
| 173 | if sep is None: |
| 174 | sep = "\t" |
| 175 | buf = StringIO() |
| 176 | |
| 177 | # clipboard_set (pyperclip) expects unicode |
| 178 | obj.to_csv(buf, sep=sep, encoding="utf-8", **kwargs) |
| 179 | text = buf.getvalue() |
| 180 | |
| 181 | clipboard_set(text) |
| 182 | return |
| 183 | except TypeError: |
| 184 | warnings.warn( |
| 185 | "to_clipboard in excel mode requires a single character separator.", |
| 186 | stacklevel=find_stack_level(), |
| 187 | ) |
| 188 | elif sep is not None: |
| 189 | warnings.warn( |
| 190 | "to_clipboard with excel=False ignores the sep argument.", |
| 191 | stacklevel=find_stack_level(), |
| 192 | ) |
nothing calls this directly
no test coverage detected