Convert simple lines on clipboard to bulleted lines using Pyperclip in Python

Suppose, you have a big text document file full of text. Someone assigns you, a task to add the numerical bullets at the beginning of every line. Now. what will you do? Are you going to add numbers one by one in every line? No, you will haven’t to do this. Then what’s the solution?

Here in Python, we have a very simple module named ‘Pyperclip’. With the help of this module, you can do it very easily. Read this article to know, How to do that?

Introduction of Pyperclip

Pyperclip is a very simple cross-platform module to copy and paste text from the clipboard. After copying the text, it’s very easy to modify.

 

How to install Pyperclip-

Windows/Linux- pip install pyperclip

Jupyter Notebook- !pip install pyperclip(type this in a cell)

Use of Pyperclip in Python

First of all, we have to import our module with the alias of pc and then we paste the text in clipboard_text variable using paste() method of the module.

Note:- It will paste the text which is already copied in your clipboard.

import pyperclip as pc 

clipboard_text = pc.paste()

check whether the text is correct or not.

print(clipboard_text)
In this tutorial we will learn,
about the pyperclip module.
This is vey simple and,
cross-platform module.
This is used for copy and,
paste from or on clipboard.

Python program to make bulleted lines from simple lines

After the above segment of code, we split this poem from where it breaks the line using split() method and store it to the cliboard_list list variable. Next, we iterate our list to modify it by adding a numerical bullet to every index of the list.

clipboard_list =clipboard_text.split("\n")
for i in range(len(clipboard_list)):
    clipboard_list[i] = str(i+1)+" "+clipboard_list[i]

Output after this segment of code text variable converted into a list of lines.

print(clipboard_list)
['1.In this tutorial we will learn,',
 '2.about the pyperclip module.',
 '3.This is vey simple and,',
 '4.cross-platform module.',
 '5.This is used for copy and,',
 '6.paste from or on clipboard.']

Now, we have to join this list in a single string variable. Here, we use the join method. After this again we will
copy this string to the clipboard and print the string. (you can use it anywhere you want by simply paste)

text = "\n".join(clipboard_list)
pc.copy(text)
print(pc.paste())

Output:

1.In this tutorial we will learn,
2.about the pyperclip module.
3.This is vey simple and,
4.cross-platform module.
5.This is used for copy and,
6.paste from or on clipboard.

Full source code:

import pyperclip as pc 

clipboard_text = pc.paste()
print(clipboard_text)

clipboard_list =clipboard_text.split("\n")
print(clipboard_list)

for i in range(len(clipboard_list)):
    clipboard_list[i] = str(i+1)+clipboard_list[i]
    
text = "\n".join(clipboard_list)
pc.copy(text)
print(pc.paste())

Also read: Find the Longest Word in a Text File in Python

Leave a Reply

Your email address will not be published.