简体   繁体   中英

Copy data from one excel sheet to another excel sheet

I was writing this script to search a number in a an excel sheet then copy the whole row associated to that number and then paste it to a new excel sheet. I wanted to copy the same data row after row and as much as I execute the program and save it. I tried to figure out the solution through many ways by searching similar problems in this community but it does not work. Below is the code.

   import xlsxwriter
   import xlrd
   import xlwt
   import math

   def open_file(path):
       wb = xlrd.open_workbook(path)
       sheet = wb.sheet_by_index(0)

       for row_num in range(sheet.nrows):
           row_value = sheet.row_values(row_num)

           if row_value[2] == 68860:
              row = 0
              col = 0

              wbk = xlsxwriter.Workbook('or.xlsx')
              ws = wbk.add_worksheet()

              ws.write('A1', row_value[0])
              ws.write('B1', row_value[1])
              ws.write('C1', row_value[2])
              row = row + 1

              wbk.close()
   if __name__ == "__main__":
       path = "ord.xlsx"
       open_file(path)

Any suggestions, I would really appreciate it.

Edited Version: I solved the issue that I was facing. Now, I couldnt find a solution to prevent breaking of my serial port connection even when the .csv file is already open. Here is the explaination of the main background.

Actually the opiginal idea was to search the number by scanning a QR code containing that particular number and then write information to a new file if the number is found. I solved this problem by writing it to a .csv file instead of an Xlsx file. Now everything works fine except one problem which I am not able to crack it. I have connected my scanner via serial port. There would be many users who would open and access this .csv file any time. When the program is running and the file is closed the scanning part is working fine. But when the file is open and someone scans any qr code, certainly it will give an error that the .csv file is open and along with that the whole program breaks out and we have to run it again and again. Is there any possibility to how prevent the program breaking out and just continue even if the file is open ? Below is my code.

if(ser.isOpen()):
ser.flushInput()
ser.flushOutput()

while(1):

    y = ser.readline().decode("ASCII")

    if (str(y)!=''):
        if (str(y)!='\r'):
            if (str(y)!='\n'):
                print(y)
                for row_num in range(sheet.nrows):

                    row_value = sheet.row_values(row_num)

                    if int(y) == int(row_value[0]):

                        if re.match("(11\d+)", row_value[0]) is not None:

                            file = open('Function.csv', 'a')
                            file.write(str(row_value[0])+";"+str(row_value[1])+"\n")

                            file.close()
                            ser.close()

There is a but in your code. Each time you find that value (and they may be multiple rows with that vlaue, right?), you will write to new sheet.

If you open destination workbook before loop, and keep track of which row you should write next to you will get all those values into singe sheet.

Something like this snippet that use string interpolation:

def open_file(path):
   row_in_output = 1
   wbk = xlsxwriter.Workbook('or.xlsx')
   ws = wbk.add_worksheet()
   [...]
          ws.write("A{row_in_output}", row_value[0])
          ws.write("B{row_in_output}", row_value[1])
          ws.write("C{row_in_output}", row_value[2])
          row = row + 1
          row_in_output = row_in_output + 1

          wbk.close()

PS I'm using Python 3.6 interpolation syntax. Adjust accordingly if you use older Python.

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