Function to download file from Google Spreadsheet and then convert it into text files as according to our requirement after reading from Excel File downloaded from Google SpreadSheet.
Sample usage : python lang_parsing.py(path where we want to store our files)
Note : I am assuming spreadsheet doc name "Music App Static Labels List v b1.0" and Sheet name "Sheet1". If this name changed in doc then change also has to be done in code to change name else all functions are generic.
##################### Start of the Program
#!/usr/bin/python
import os
import xlrd
import sys
import time
import datetime
dir_path=sys.argv[1]
## Iterating over the Excel sheet given in the Google spreadsheet
def iter_workbook(xls_file):
workbook = xlrd.open_workbook(dir_path + "/" + xls_file)
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
num_cells=worksheet.ncols-1
open_file(num_rows,num_cells,worksheet)
## Function to open file in write mode as language given in Excel Sheet
def open_file(num_rows,num_cells,worksheet):
i=0
while num_cells>=0:
file_name=worksheet.cell_value(0,i)
file_obj=open(dir_path + "/" + file_name+".txt","w")
write_file(num_rows,worksheet,file_obj,i)
file_obj.close()
num_rows=worksheet.nrows - 1
num_cells=num_cells - 1
i=i+1
## Function to write file with the language name as given in Excel Sheet
def write_file(num_rows,worksheet,file_obj,i):
j=1
while num_rows>0:
obj='"'+worksheet.cell_value(j,0)+'"="'+worksheet.cell_value(j,i)+'"'
file_obj.write(obj.encode('utf8'))
file_obj.write("\n")
j=j+1
num_rows=num_rows - 1
## Function to check Excel file downloaded from Google Spreadsheet
def calling_func():
postfix = "xls"
for xls_file in os.listdir(dir_path):
if not xls_file.lower().endswith( postfix ):
continue
else:
iter_workbook(xls_file)
## Function to download file from Google Spreadsheet with python inbuilt module googlecl
def downl_xls():
os.system("google docs get --title='Music App Static Labels List v b1.0' --dest=languages ")
try:
os.system("cp languages.xls " + dir_path + " ")
calling_func()
except:
print "Mentioned directory doesn't exist"
if __name__=="__main__":
downl_xls()
######################## End of the Program
Sample usage : python lang_parsing.py
Note : I am assuming spreadsheet doc name "Music App Static Labels List v b1.0" and Sheet name "Sheet1". If this name changed in doc then change also has to be done in code to change name else all functions are generic.
##################### Start of the Program
#!/usr/bin/python
import os
import xlrd
import sys
import time
import datetime
dir_path=sys.argv[1]
## Iterating over the Excel sheet given in the Google spreadsheet
def iter_workbook(xls_file):
workbook = xlrd.open_workbook(dir_path + "/" + xls_file)
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
num_cells=worksheet.ncols-1
open_file(num_rows,num_cells,worksheet)
## Function to open file in write mode as language given in Excel Sheet
def open_file(num_rows,num_cells,worksheet):
i=0
while num_cells>=0:
file_name=worksheet.cell_value(0,i)
file_obj=open(dir_path + "/" + file_name+".txt","w")
write_file(num_rows,worksheet,file_obj,i)
file_obj.close()
num_rows=worksheet.nrows - 1
num_cells=num_cells - 1
i=i+1
## Function to write file with the language name as given in Excel Sheet
def write_file(num_rows,worksheet,file_obj,i):
j=1
while num_rows>0:
obj='"'+worksheet.cell_value(j,0)+'"="'+worksheet.cell_value(j,i)+'"'
file_obj.write(obj.encode('utf8'))
file_obj.write("\n")
j=j+1
num_rows=num_rows - 1
## Function to check Excel file downloaded from Google Spreadsheet
def calling_func():
postfix = "xls"
for xls_file in os.listdir(dir_path):
if not xls_file.lower().endswith( postfix ):
continue
else:
iter_workbook(xls_file)
## Function to download file from Google Spreadsheet with python inbuilt module googlecl
def downl_xls():
os.system("google docs get --title='Music App Static Labels List v b1.0' --dest=languages ")
try:
os.system("cp languages.xls " + dir_path + " ")
calling_func()
except:
print "Mentioned directory doesn't exist"
if __name__=="__main__":
downl_xls()
######################## End of the Program
No comments:
Post a Comment