Python enables replication between worksheets sheet across Excel

Source: Internet
Author: User
Tags python script

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.