Last active
April 4, 2022 15:52
-
-
Save kiyoon/1a4edb2a306040e38bca3398ffe4732e to your computer and use it in GitHub Desktop.
Copy list of numbers to clipboard so you can paste on Excel sheets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Requirements | |
# pip3 install pandas | |
# sudo apt install xclip | |
import pandas as pd | |
import numpy as np | |
import argparse | |
def is_float(element: str) -> bool: | |
try: | |
float(element) | |
return True | |
except ValueError: | |
return False | |
def get_parser(): | |
parser = argparse.ArgumentParser(description="Copy list of numbers to clipboard so you can paste on Excel easily. Only support 1 dimensional (horizontal or vertical).", | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument("numbers", nargs='+', help="") | |
parser.add_argument("-T", "--include_text", action='store_true', help="If False, it will remove all text and include only numbers.") | |
parser.add_argument("-v", "--vertical", action='store_true', help="Copy vertically.") | |
parser.add_argument("-s", "--separator", default=' ', help="Separator of the numbers") | |
return parser | |
def main(): | |
parser = get_parser() | |
args = parser.parse_args() | |
# parse all elements in args.numbers list | |
numbers = [] | |
for number in args.numbers: | |
numbers.extend(number.split(args.separator)) | |
# remove text | |
if not args.include_text: | |
numbers = [number for number in numbers if is_float(number)] | |
data = np.array(numbers, ndmin=2) | |
if args.vertical: | |
data = data.transpose() | |
df = pd.DataFrame(data) | |
df.to_clipboard(excel=True, index=False, header=None) | |
print(f'Copied to clipboard: {numbers}') | |
print(f'{"Vertical" if args.vertical else "Horizontal"}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment