简体   繁体   中英

How do you copy cell by cell with Python xlwings from one file to another

I was using openpyxl but found it can not handle macro-based Excel files. I need to know how to copy certain cells from one excel file to another with xlwings, which can handle macros. The syntax is not the same and i am not sure how to go about doing that cell by cell accounting or rows and columns. Any sample code would be much appreciated!

for the ease to read and write data, you can use the below function to read and write

import xlwings as xw

   def readData(rownum, column_name):
        rownum1 = str(rownum) 
        cell = column_name+rownum1
        return sheet.range(cell).value

    def writeData(rownum, column_name, data):
        rownum1 = str(rownum)
        cell = column_name+ str(rownum)
        sheet2.range(cell).value= data

first make an object and open your excel file from where you need to read and write and define worksheet

wb_reader = xw.Book(path_file_ of_read)

sheet = workbook.sheets["Sheet1"] #for reading

wb_writer = xw.Book(path_file_ of_write)

sheet2 = workbook.sheets["Sheet2"] #for writing

and now just do

x = readData(2,3)
write(2,3,x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM