Python to delete a row in excel spreadsheet

前端 未结 6 630
走了就别回头了
走了就别回头了 2021-01-20 03:21

I have a really large excel file and i need to delete about 20,000 rows, contingent on meeting a simple condition and excel won\'t let me delete such a complex range when us

相关标签:
6条回答
  • 2021-01-20 03:39

    Don't delete. Just copy what you need.

    1. read the original file
    2. open a new file
    3. iterate over rows of the original file (if the first column of the row does not contain the value X, add this row to the new file)
    4. close both files
    5. rename the new file into the original file
    0 讨论(0)
  • 2021-01-20 03:40

    If you just need to delete the data (rather than 'getting rid of' the row, i.e. it shifts rows) you can try using my module, PyWorkbooks. You can get the most recent version here:

    https://sourceforge.net/projects/pyworkbooks/

    There is a pdf tutorial to guide you through how to use it. Happy coding!

    0 讨论(0)
  • 2021-01-20 03:42

    I like using COM objects for this kind of fun:

    import win32com.client
    from win32com.client import constants
    
    f = r"h:\Python\Examples\test.xls"
    DELETE_THIS = "X"
    
    exc = win32com.client.gencache.EnsureDispatch("Excel.Application")
    exc.Visible = 1
    exc.Workbooks.Open(Filename=f)
    
    row = 1
    while True:
        exc.Range("B%d" % row).Select()
        data = exc.ActiveCell.FormulaR1C1
        exc.Range("A%d" % row).Select()
        condition = exc.ActiveCell.FormulaR1C1
    
        if data == '':
            break
        elif condition == DELETE_THIS:
            exc.Rows("%d:%d" % (row, row)).Select()
            exc.Selection.Delete(Shift=constants.xlUp)
        else:
            row += 1
    
    # Before
    # 
    #      a
    #      b
    # X    c
    #      d
    #      e
    # X    d
    #      g
    #        
    
    # After
    #
    #      a
    #      b
    #      d
    #      e
    #      g
    

    I usually record snippets of Excel macros and glue them together with Python as I dislike Visual Basic :-D.

    0 讨论(0)
  • 2021-01-20 03:50

    I have achieved this using Pandas package....

    import pandas as pd
    
    #Read from Excel
    xl= pd.ExcelFile("test.xls")
    
    #Parsing Excel Sheet to DataFrame
    dfs = xl.parse(xl.sheet_names[0])
    
    #Update DataFrame as per requirement
    #(Here Removing the row from DataFrame having blank value in "Name" column)
    
    dfs = dfs[dfs['Name'] != '']
    
    #Updating the excel sheet with the updated DataFrame
    
    dfs.to_excel("test.xls",sheet_name='Sheet1',index=False)
    
    0 讨论(0)
  • 2021-01-20 03:54

    You can use,

    sh.Range(sh.Cells(1,1),sh.Cells(20000,1)).EntireRow.Delete()
    

    will delete rows 1 to 20,000 in an open Excel spreadsheet so,

    if sh.Cells(1,1).Value == 'X':
    
       sh.Cells(1,1).EntireRow.Delete()
    
    0 讨论(0)
  • 2021-01-20 03:56

    You can try using the csv reader:

    http://docs.python.org/library/csv.html

    0 讨论(0)
提交回复
热议问题