How can I copy one excel file data to another excel file by excluding hidden rows content using Python

Karim kliateni at gmail.com
Wed Oct 19 07:34:07 EDT 2016


On 19/10/2016 11:30, Kishore JK wrote:
> I need to copy one excel file data into another excel file by excluding rows which were hidden in source excel file.
>
> https://i.stack.imgur.com/NPUK6.png
>
> As shown in the image, from the source excel file, I need to copy the data of row numbers 116,135 and 139 and exclude all the remaining rows which were hidden because of not matching the criteria.
>
> I have tried below code, but this is copying entire data into new excel sheet.
> wb = openpyxl.load_workbook('sourcefile.xlsx')
> sheet = wb.active
> sheet.title = 'Sheet1'
> wb.save('destinationfile.xlsx')

Hello,

I use xlrd external module. This is only samples codes from one  of my 
class. You  can filter the row you want with an conditional statement.
You must have equivalent attribute in openpyxl module.

Import xlrd

Import csv

  self._book                = xlrd.open_workbook(xlfilename)

  self._selected_sheets = [self._book.sheet_by_name(sheetname) for 
sheetname in sheets]

   writer = csv.writer(open(xlfilename, 'wb'), 
delimiter=self.file_format.delimiter)

   rows   = izip(*(sheet.col_values(col) for col in selected_columns))

   for row in rows:

        # NOTE!!!: could have UnicodeEncodeError exception raised here!

        try:

               writer.writerow(row)

        except UnicodeEncodeError, e:

                print("\nWarning: {0}. Row '{1}' will not be 
extracted!".format(e, row))


Regards

Karim





More information about the Python-list mailing list