|
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 |
0 commit comments