Baidu search problem: Python copy sheet in Excel to another workbook
One of the articles:
Address: 72765116
Python enables replication between worksheets sheet across Excel
Python, copy the Test1 Sheet1 through the "cross-file" to Test2 Sheet2.
Including Google has not been able to search out the answer to this question.
We post the code.
We load OPENPYXL This package to solve:
From OPENPYXLImport Load_workbookfilename =' test1.xlsx ' filename2 =' Test2.xlsx 'def replace_xls (sheetname): WB = Load_workbook (filename) wb2 = Load_workbook (filename2) ws = Wb[sheetname] ws2 = wb2[sheetname] #两个for循环遍历整个excel的单元格内容 for i,row in Enumerate (ws.iter_rows ()): for j,cell in Enumerate (Row): Ws2.cell (Row=i+1, Column=j+1, Value=cell.value) wb2.save (filename2) sheetnames = [u ' Sheet1 ', u ' Sheet2 ', u ' Sheet3 ', u ' Sheet4 '] #遇到复制几十个sheet时候, it is necessary to write a loop in Sheetnames:replace_xls (sheetname)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
Note that my code overwrites the contents of the original Excel.
How your Excel is dynamic, you can write a VB script yourself, first empty Excel before you run the Python script.
Finally, please refer to the documentation for this package OPENPYXL:
Https://media.readthedocs.org/pdf/openpyxl/latest/openpyxl.pdf
Python enables replication between worksheets sheet across Excel