| 1 |
"""
|
| 2 |
Excel Empty Row Remover
|
| 3 |
|
| 4 |
Created by
|
| 5 |
Emanuel Borges
|
| 6 |
10.30.2015
|
| 7 |
|
| 8 |
Problem:
|
| 9 |
An issue came up with thousands of Excel files where they needed to be tiffed but because they had
|
| 10 |
millions of rows of empty but not technically empty data, they would tiff out in the millions of pages.
|
| 11 |
Since there was formatting on those rows, LAW wouldnt remove the empty pages. Imaging in Relativity
|
| 12 |
also didnt work, since it would just error out.
|
| 13 |
|
| 14 |
Solution: This program will take a directory of Excel files, open them, using COM, and do the following, for every
|
| 15 |
sheet in the document:
|
| 16 |
* Unhide all rows and columns
|
| 17 |
* Determin the largest sheet area both for rows and columns
|
| 18 |
* Hide these "empty" rows.
|
| 19 |
* Save as a new version of the file.
|
| 20 |
|
| 21 |
"""
|
| 22 |
|
| 23 |
import os
|
| 24 |
import win32com.client as win32
|
| 25 |
from win32com.client import constants as c
|
| 26 |
|
| 27 |
if __name__ == '__main__':
|
| 28 |
startDir = r""
|
| 29 |
|
| 30 |
## Remove this after beta to loop through all files instead.
|
| 31 |
filePathAndName = r'c:\test\test.xlsx'
|
| 32 |
|
| 33 |
excel = win32.gencache.EnsureDispatch('Excel.Application')
|
| 34 |
excel.Application.ScreenVisible = False
|
| 35 |
wb = excel.Workbooks.Open(filePathAndName)
|
| 36 |
excel.Application.ScreenUpdating = False
|
| 37 |
|
| 38 |
sheetCount = wb.Sheets.Count
|
| 39 |
for sheetNumb in range(1,sheetCount+1):
|
| 40 |
ws = wb.Worksheets(sheetNumb)
|
| 41 |
lastrow = ws.Cells.Find(What="*",After=ws.Range("A1"),LookAt=c.xlPart,LookIn=c.xlFormulas,SearchOrder=c.xlByRows,SearchDirection=c.xlPrevious,MatchCase=False).Row
|
| 42 |
emptyList = []
|
| 43 |
lastcol = ws.Cells.Find(What="*",After=ws.Range("A1"),LookAt=c.xlPart,LookIn=c.xlFormulas,SearchOrder=c.xlByRows,SearchDirection=c.xlPrevious,MatchCase=False).Column
|
| 44 |
print "working on worksheet %s. %s rows"%(sheetNumb,lastrow)
|
| 45 |
|
| 46 |
print "Unhiding all rows..."
|
| 47 |
## Unhiding all rows
|
| 48 |
ws.Rows.EntireRow.Hidden = False
|
| 49 |
print "Done."
|
| 50 |
print "Unhiding all columns..."
|
| 51 |
## Unhiding all columns
|
| 52 |
ws.Columns.EntireRow.Hidden = False
|
| 53 |
print "Done."
|
| 54 |
|
| 55 |
print "processing sheet..."
|
| 56 |
## got to be a better way to do this.
|
| 57 |
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
| 58 |
result = []
|
| 59 |
while lastcol:
|
| 60 |
lastcol, rem = divmod(lastcol-1, 26)
|
| 61 |
result[:0] = LETTERS[rem]
|
| 62 |
r = ws.Range("A1:%s"%''.join(result)+str(lastrow))
|
| 63 |
rows = r.Rows.Count
|
| 64 |
emptyRowBeg = False
|
| 65 |
for i in range(1,rows+1):
|
| 66 |
if excel.WorksheetFunction.CountA(r.Rows(i)) == 0:
|
| 67 |
if emptyRowBeg:
|
| 68 |
pass
|
| 69 |
else:
|
| 70 |
emptyRowBeg = i
|
| 71 |
else:
|
| 72 |
if emptyRowBeg:
|
| 73 |
emptyList.append(("%s,%s")%(emptyRowBeg,i-1))
|
| 74 |
emptyRowBeg = False
|
| 75 |
for x in emptyList:
|
| 76 |
beg,end = x.split(",")
|
| 77 |
ws.Rows("%d:%d"%(int(beg),int(end))).EntireRow.Hidden = True
|
| 78 |
|
| 79 |
|
| 80 |
for i in range(1,rows+1):
|
| 81 |
if excel.WorksheetFunction.CountA(r.Rows(i)) == 0:
|
| 82 |
if emptyRowBeg:
|
| 83 |
pass
|
| 84 |
else:
|
| 85 |
emptyRowBeg = i
|
| 86 |
else:
|
| 87 |
if emptyRowBeg:
|
| 88 |
emptyList.append(("%s,%s")%(emptyRowBeg,i-1))
|
| 89 |
emptyRowBeg = False
|
| 90 |
for x in emptyList:
|
| 91 |
beg,end = x.split(",")
|
| 92 |
ws.Rows("%d:%d"%(int(beg),int(end))).EntireRow.Hidden = True
|
| 93 |
|
| 94 |
print "Sheet processed."
|
| 95 |
print "Saving..."
|
| 96 |
pth,ext = filePathAndName
|
| 97 |
## ALWAYS SAVE THE WORKBOOK AND NOT JUST THE SHEET. WB NOT WS
|
| 98 |
wb.SaveAs(pth + "Fixed"+ext)
|
| 99 |
wb.Close()
|
| 100 |
print "Saved and closed." |