python batch processing excel

#!/bin/env python

# coding:utf-8

import sys

#import csv

#import re

import them

#import collections as col

from openpyxl import load_workbook

import xlwt

reload(sys)

sys.setdefaultencoding("utf8")

path="/kaka/excel/file/11"

filelist=os.listdir(path)

dates = []

for i in range(len(filelist)):

    ft = path+"/"+filelist[i]

    ws = load_workbook(ft)

    sheet = ws.get_sheet_by_name("Basic Information Registration Form")

    datas.append([sheet.cell(row=4,column=4).value,sheet.cell(row=5,column=4).value,sheet.cell(row=6,column=4).value,sheet.cell(row=6,column=6).value,sheet.cell(row=7,column=4).value,sheet.cell(row=7,column=6).value,sheet.cell(row=8,column=4).value,sheet.cell(row=8,column=6).value,sheet.cell(row=9,column=4).value,sheet.cell(row=9,column=6).value,sheet.cell(row=10,column=4).value,sheet.cell(row=10,column=6).value,sheet.cell(row=11,column=4).value,sheet.cell(row=11,column=6).value,sheet.cell(row=12,column=4).value,sheet.cell(row=12,column=6).value,sheet.cell(row=13,column=4).value,sheet.cell(row=13,column=6).value,sheet.cell(row=14,column=4).value,sheet.cell(row=14,column=6).value,sheet.cell(row=15,column=4).value,sheet.cell(row=15,column=6).value,sheet.cell(row=16,column=4).value,sheet.cell(row=16,column=6).value,sheet.cell(row=17,column=4).value,sheet.cell(row=17,column=6).value,sheet.cell(row=18,column=4).value,sheet.cell(row=18,column=6).value,sheet.cell(row=19,column=4).value,sheet.cell(row=19,column=6).value,sheet.cell(row=20,column=4).value,str(sheet.cell(row=20,column=6).value)])

def write_excel(datas):

    sheet_name='Sheet1'

    '''

        1: Standard parcel coding...

    '''

    row0=['1', '2', '3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32']

    file_name="work.xls"

    # Create a write object instance

    f = xlwt.Workbook()

    # Create a table name called sheet_name

    sheet1 = f.add_sheet(sheet_name, cell_overwrite_ok=True)

    # Create the first row (title) of the sheet_name

    for i in range(0, len(row0)):

        sheet1.write(0, i, row0[i])

    # Write data from the second line, write as many lines as there are elements in datas

    for row in range(1, len(datas) + 1):

        for col in range(len(row0)):

            print datas[row-1][col]

            # With the row number and column number provided by row and col above, you can exactly match a cell, and start writing data to the cell below

            # row: row number, col: column number, datas[row-1][col]: data

            # Note: One element in the list of datas is a row. Since it is written from the second row, row-1 is required to get the first element of datas

            # Note: An element in the datas list (list) corresponds to the value of each column in this row.

            sheet1.write(row, col, datas[row-1][col])

    f.save(file_name)

    print('The excel file has been generated!')

if __name__ == '__main__':

    write_excel(datas)


Guess you like

Origin blog.51cto.com/12768454/2542935