简体   繁体   English

将数据从一个 Excel 工作表复制到另一个 Excel 工作表

[英]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.我正在编写此脚本以在 Excel 工作表中搜索一个数字,然后复制与该数字关联的整行,然后将其粘贴到一个新的 Excel 工作表中。 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.现在,即使 .csv 文件已经打开,我也找不到防止中断串行端口连接的解决方案。 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.我通过将其写入 .csv 文件而不是 Xlsx 文件解决了这个问题。 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.会有很多用户随时打开和访问这个 .csv 文件。 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.但是当文件打开并且有人扫描任何二维码时,肯定会给出.csv文件打开的错误,并且整个程序会崩溃,我们必须一次又一次地运行它。 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. PS 我正在使用 Python 3.6 插值语法。 Adjust accordingly if you use older Python.如果您使用较旧的 Python,请相应地进行调整。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 使用Python(和DataNitro)将单元格从一个Excel工作簿中的特定工作表复制到另一个Excel工作簿中的特定工作表 - Using Python (and DataNitro) to copy cells from a particular sheet in one Excel workbook, to a particular sheet in another Excel workbook 在Python中将excel工作表从一个工作表复制到另一个工作表 - Copy excel sheet from one worksheet to another in Python 如何使用 pandas 将数据从一张 excel 工作表循环复制到另一个具有相同文件名的工作表 - How to looping copy data from one excel sheet to another with same filename with pandas 使用 python 将列从一个 excel 文件复制到另一个 excel 文件表 - Copy columns from one excel file to another excel file sheet using python 从一个工作表复制数据,然后将其粘贴到另一个Excel工作表时,Openpyxl边界线中断 - Openpyxl border line breaks when copying data from one sheet and then pasting it to another excel sheet 如何使用 openpyxl 库将列从一张 excel 表复制到另一张 python? - How can I copy columns from one excel sheet to another with python using openpyxl Library? 将数据从多个 excel 文件复制到特定工作表上的现有文件 - Copy Data from multiple excel files to existing file on specific sheet 从Excel工作表进行数据排序 - Data sorting from Excel sheet 从Excel工作表中获取数据 - Getting data from an Excel sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM