Skip to main content

Python script: Copy information from excel in one go

Completion date: March 15, 2019

This version 4 adds the try and except error.  Better coding, it should always work (hopefully).  The dimension of the application is set, and the position of the application (where to appear on the screen) is also set.



==================================
#! python 3.6.4 -- Modified on Mar 15 2019 (by Bonnie Chan)

import pyperclip
import openpyxl
import os
from tkinter import *
from tkinter import messagebox

#Get the workbook, worksheet
file = "AR.xlsx"
wb = openpyxl.load_workbook(file)
ws = wb['Sheet1']

#Customer name, salesman, Project name, Job no, PO no, Service amount
#column_list_letter = ['I', 'G', 'H', 'Q', 'P', 'N']
column_list_for_content = [9, 7, 8, 17, 16, 14]
column_list_for_subj = [[9, " - "], [8, " ("], [17, ")"]]

root = Tk()
root.title('Copycat v4.0')
root.geometry("300x150+400+400")

lbl = Label(root, text="Which row?")
lbl.pack()

user_input = Entry(root)
user_input.pack()

btn_copysubj = Button(root, text="Copy Subject", command=lambda: copy_subj())
btn_copysubj.pack()

btn_copycontent = Button(root, text="Copy Content", command=lambda: copy_content())
btn_copycontent.pack()

def copy_subj():
    try:
        user_input_row = int(user_input.get())

        content = ""

        for c in column_list_for_subj:
            # Get the specific row info and append to the content
            content = content + str(ws.cell(row=user_input_row, column=c[0]).value)

            # Add the special characters
            content = content + c[1]

        #copy the content to clipboard
        pyperclip.copy(content)
        messagebox.showinfo("Copycat", "Subject Copied.")
 
    except:
        messagebox.showwarning("Warning", "Your input was not a valid row number.")

def copy_content():
    try:
        user_input_row = int(user_input.get())

        content = ""

        for c in column_list_for_content:
            # Get the first row headers
            content = content + ws.cell(row=1, column=c).value + ": "

            # Get the specific row info and append to the content
            content = content + str(ws.cell(row=user_input_row, column=c).value)

            # Add the carriage return
            content = content + "\r\n"

        #copy the content to clipboard
        pyperclip.copy(content)
        messagebox.showinfo("Copycat", "Content Copied.")

    except:
        messagebox.showwarning("Warning", "Your input was not a valid row number.")

root.mainloop()

==========END==========


Completion date: March 14, 2019

This version 3 adds options to copy the email subject line or the email content information.


==================================

#! python 3.6.4 -- Modified on Mar 14 2019

import pyperclip
import openpyxl
import os
from tkinter import *
from tkinter import messagebox

#Get the workbook, worksheet
file = "AR.xlsx"
wb = openpyxl.load_workbook(file)
ws = wb['Sheet1']

#Customer name, salesman, Project name, Job no, PO no, Service amount
#column_list_letter = ['I', 'G', 'H', 'Q', 'P', 'N']
column_list_for_content = [9, 7, 8, 17, 16, 14]
column_list_for_subj = [[9, " - "], [8, " ("], [17, ")"]]

root = Tk()
root.title('Copycat')

lbl = Label(root, text="Which row?")
lbl.pack()

user_input = Entry(root)
user_input.pack()

btn_copysubj = Button(root, text="Copy Subject", command=lambda: copy_subj())
btn_copysubj.pack()

btn_copycontent = Button(root, text="Copy Content", command=lambda: copy_content())
btn_copycontent.pack()

def copy_subj():
    user_input_row = int(user_input.get())

    content = ""

    for c in column_list_for_subj:
        # Get the specific row info and append to the content
        content = content + str(ws.cell(row=user_input_row, column=c[0]).value)

        # Add the special characters
        content = content + c[1]

    #copy the content to clipboard
    pyperclip.copy(content)
    messagebox.showinfo("Copycat", "Subject Copied.")
 

def copy_content():
    user_input_row = int(user_input.get())

    content = ""

    for c in column_list_for_content:
        # Get the first row headers
        content = content + ws.cell(row=1, column=c).value + ": "

        # Get the specific row info and append to the content
        content = content + str(ws.cell(row=user_input_row, column=c).value)

        # Add the carriage return
        content = content + "\r\n"

    #copy the content to clipboard
    pyperclip.copy(content)
    messagebox.showinfo("Copycat", "Content Copied.")

root.mainloop()

==========END==========



Completion date: February 21, 2019

This version 2 puts the command line input to a GUI input.  The reason is to avoid running the program over and over again.  Moreover, I can utilize GUI on an executable file.


==================================

#! python 3.6.4 -- Modified on Feb 21 2019

import pyperclip
import openpyxl
import os
from tkinter import *
from tkinter import messagebox

#Get the workbook, worksheet
file = "AR.xlsx"
wb = openpyxl.load_workbook(file)
ws = wb['Sheet1']

#Customer name, salesman, Project name, Job no, PO no, Service amount
#column_list_letter = ['I', 'G', 'H', 'Q', 'P', 'N']
column_list = [9, 7, 8, 17, 16, 14]

root = Tk()
root.title('Copycat')

lbl = Label(root, text="Which row?")
lbl.pack()

user_input = Entry(root)
user_input.pack()

btn_ok = Button(root, text="OK", command=lambda: copy_content())
btn_ok.pack()

def copy_content():
    user_input_row = int(user_input.get())

    content = ""

    for c in column_list:
        # Get the first row headers
        content = content + ws.cell(row=1, column=c).value + ": "

        # Get the specific row info and append to the content
        content = content + str(ws.cell(row=user_input_row, column=c).value)

        # Add the carriage return
        content = content + "\r\n"

    #copy the content to clipboard
    pyperclip.copy(content)
    messagebox.showinfo("Copycat", "Copied.")

root.mainloop()

==========END==========



Completion date: February 17, 2019

My new job duty requires me to check with other parties enquiring the progress of project cases.   I process about 40 new cases every month and I often need to email them if I couldn't reach the project managers by phone.  For every new case I am emailing, I have to gather all the information from my excel file (Customer name, salesman, Project name, Job no, PO no, Service amount)...I need to find a fast copy solution.  Basically I am just copying the first header row and the data information in a specific row (specified by user).  Here is the first draft.

==================================
#! python 3.6.4 -- Created on Feb 17 2019

import pyperclip
import openpyxl
import os

#Get the workbook, worksheet
file = "AR.xlsx"
wb = openpyxl.load_workbook(file)
ws = wb['Sheet1']

#Customer name, salesman, Project name, Job no, PO no, Service amount
#column_list_letter = ['I', 'G', 'H', 'Q', 'P', 'N']
column_list = [9, 7, 8, 17, 16, 14]

user_input = input("Which row?")
user_input_row = int(user_input)

content = ""

for c in column_list:
    # Get the first row headers
    content = content + ws.cell(row=1, column=c).value + ": "

    # Get the specific row info and append to the content
    content = content + str(ws.cell(row=user_input_row, column=c).value)

    # Add the carriage return
    content = content + "\r\n"

#copy the content to clipboard
pyperclip.copy(content)
print("Copied to clipboard.")

==========END==========

Comments

Popular posts from this blog

DIY Keyboard drawer installation

Completion Date: May 24, 2020 Work from home has given me chance to use my DIY table (35cm*50cm) ever more than before.  I feel great, but I realize the fact that I don’t have enough space if I want to work on my iPad at the same time.  I bought this mini drawer tracks so I can create this space. Material:  - 10 inch (25cm) keyboard drawer tracks, 1 pair (Taobao, RMB12.5) - 24*36cm wooden board (left over I have at home) Steps: 1.  Stretch the keyboard tracks to full extent.  Fit the wooden board to the end of the track and mark the holes with pencil. 2. This is 2.5mm drill bit.  Drill pilot holes to the marked holes.  Do the same for the other side. 3. Take my table top out and flip upside down.  Fit the keyboard drawer and trace the outline and mark the holes with pencil.  Re-verify the keyboard outer edge aligns in parallel with the table top’s outer edge. 4. Drill the holes and screws in to fix the keyboard drawer.  T

Gear investigation: Banbao Lego pirate ship on a swing

Completion date: May 14, 2020 1. Gear ratio is 16:24. The top spinning is at 0.66x of motor. I thought that the black gear connection just pass on same speed, but by experiment, the speed gears has the same effect just like when they are in contact. 2. By connecting the gears via the connection links, the gears turn in the same direction. 3. By connecting the gears via the connection links, it solves the distance problem between the two gears with a simple solution.  Without the connection links, many gears would be needed to get to the top. 4. I think this pirate ship is a tricky one, because one would have to tune the spinning speed to match the latency of the ship for the swinging motion to be smooth left-right-left-right. How to Give the push at the right time?  Also, the motor and the gears can get damaged when the motion is always interrupted.

Star cushion

Completion date: Jun 12 2020 Steps 1. Trace the star shape onto the fabric.  Double over the fabric.  Apply pins to secure the edges.  Cut out. 2. Install a platform on the sewing machine.  3. Whenever I need to turn the fabric I place the needle down on the fabric before turning the fabric.  Sew until I get a 15 cm opening. 4. Flip the fabric inside out and put the foam filling inside the star cushion.  Bind off the opening by hand sewing. My daughter requested for a star cushion.  I recycled filling of a foam-type cushion into this one.  This was the first time I used a platform.  The table was not long enough but I put something underneath to support the platform.  She was pleased with it though it wasn’t pink as she originally requested.  Below in the picture was her little hand!