Skip to content

Commit a5a7709

Browse files
authored
Add files via upload
1 parent 1fd4dca commit a5a7709

File tree

5 files changed

+64
-57
lines changed

5 files changed

+64
-57
lines changed

copyPaste.py

+40-40
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
'''
2-
Copies A2:C4 from worksheet1.xlsx to new row(s) starting at the end of worksheet2.xlsx, then saves it as worksheet3.xlsx
3-
'''
4-
5-
from win32com.client import Dispatch
6-
wkbk1 = "C:\\path\\to\\worksheet1.xlsx"
7-
wkbk2 = "C:\\path\\to\\worksheet2.xlsx"
8-
wkbk3 = "C:\\path\\to\\worksheet3.xlsx"
9-
excel = Dispatch("Excel.Application")
10-
excel.Visible = 1
11-
excel.ScreenUpdating = False
12-
13-
# copy from source
14-
source = excel.Workbooks.Open(wkbk1)
15-
excel.Range("A2:C4").Select()
16-
excel.Selection.Copy()
17-
18-
# paste (appended) to target
19-
target = excel.Workbooks.Open(wkbk2)
20-
21-
ws = target.sheets(1)
22-
used = ws.UsedRange
23-
maxrow = used.Row + used.Rows.Count - 1
24-
maxcol = used.Column + used.Columns.Count - 1
25-
newrow = maxrow + 1
26-
27-
destrange = "A"+str(newrow) #+":C9"
28-
print(destrange)
29-
30-
excel.Range(destrange).Select()
31-
excel.Selection.PasteSpecial(Paste=-4122)
32-
33-
# Excel requires the user to press enter after paste()
34-
# It says "select destination and press enter or choose paste"
35-
# This simulates pressing of the enter key:
36-
shell = Dispatch("WScript.Shell")
37-
shell.SendKeys("{ENTER}", 0)
38-
39-
excel.ScreenUpdating = True
40-
ws.SaveAs(wkbk3)
1+
'''
2+
Copies A2:C4 from worksheet1.xlsx to new row(s) starting at the end of worksheet2.xlsx, then saves it as worksheet3.xlsx
3+
'''
4+
5+
from win32com.client import Dispatch
6+
wkbk1 = "worksheet1.xlsx"
7+
wkbk2 = "worksheet2.xlsx"
8+
wkbk3 = "worksheet3.csv"
9+
excel = Dispatch("Excel.Application")
10+
excel.Visible = 1
11+
excel.ScreenUpdating = False
12+
13+
# copy from source
14+
source = excel.Workbooks.Open(wkbk1)
15+
excel.Range("A2:D4").Select()
16+
excel.Selection.Copy()
17+
18+
# paste (appended) to target
19+
target = excel.Workbooks.Open(wkbk2)
20+
21+
ws = target.sheets(1)
22+
used = ws.UsedRange
23+
maxrow = used.Row + used.Rows.Count - 1
24+
maxcol = used.Column + used.Columns.Count - 1
25+
newrow = maxrow + 1
26+
27+
destrange = "A"+str(newrow) #+":C9"
28+
print(destrange)
29+
30+
excel.Range(destrange).Select()
31+
excel.Selection.PasteSpecial(Paste=-4122)
32+
33+
# Excel requires the user to press enter after paste()
34+
# It says "select destination and press enter or choose paste"
35+
# This simulates pressing of the enter key:
36+
shell = Dispatch("WScript.Shell")
37+
shell.SendKeys("{ENTER}", 0)
38+
39+
excel.ScreenUpdating = True
40+
ws.SaveAs(wkbk3, 24) # 24 = csv format

runMacro.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import os
2-
import win32com.client
3-
4-
excelFile = "time.xlsm"
5-
macroToRun = "time.xlsm!Module1.Button1_Click"
6-
7-
if os.path.exists(excelFile):
8-
xl = win32com.client.Dispatch("Excel.Application")
9-
10-
xl.Workbooks.Open(Filename=excelFile, ReadOnly=1)
11-
# remove ", ReadOnly=1" for file save
12-
13-
xl.Application.Run(macroToRun)
14-
#xl.Application.Save() # uncomment for file save
15-
16-
xl.Application.Quit() # Comment this out if your excel script closes
17-
del xl
1+
import os
2+
import win32com.client
3+
4+
excelFile = "time.xlsm"
5+
macroToRun = "time.xlsm!Module1.Button1_Click"
6+
7+
if os.path.exists(excelFile):
8+
xl = win32com.client.Dispatch("Excel.Application")
9+
10+
xl.Workbooks.Open(Filename=excelFile, ReadOnly=1)
11+
# remove ", ReadOnly=1" for file save
12+
13+
xl.Application.Run(macroToRun)
14+
#xl.Application.Save() # uncomment for file save
15+
16+
xl.Application.Quit() # Comment this out if your excel script closes
17+
del xl

worksheet1.xlsx

3.68 KB
Binary file not shown.

worksheet2.xlsx

3.67 KB
Binary file not shown.

worksheet3.csv

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
item,count,price,total
2+
apples,1,2,2
3+
bananas,5,6,30
4+
oranges,10,11,110
5+
bacon,15,16,240
6+
hot dogs,20,21,420
7+
hamburger,25,26,650

0 commit comments

Comments
 (0)