forked from sheetjs/docs.sheetjs.com
		
	
		
			
	
	
		
			42 lines
		
	
	
		
			912 B
		
	
	
	
		
			Python
		
	
	
	
	
	
		
		
			
		
	
	
			42 lines
		
	
	
		
			912 B
		
	
	
	
		
			Python
		
	
	
	
	
	
| 
								 | 
							
								from sheetjs import SheetJS
							 | 
						||
| 
								 | 
							
								from sys import argv, exit
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								test_pandas = True
							 | 
						||
| 
								 | 
							
								try:
							 | 
						||
| 
								 | 
							
								  import pandas as pd
							 | 
						||
| 
								 | 
							
								except:
							 | 
						||
| 
								 | 
							
								  test_pandas = False
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								# Parse file and generate row objects
							 | 
						||
| 
								 | 
							
								with SheetJS() as sheetjs:
							 | 
						||
| 
								 | 
							
								  # Print library version number
							 | 
						||
| 
								 | 
							
								  print(f"SheetJS Version {sheetjs.version()}")
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  # Read and parse data from file
							 | 
						||
| 
								 | 
							
								  wb = sheetjs.read_file(argv[1])
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  # Get first worksheet name
							 | 
						||
| 
								 | 
							
								  wsname = wb.sheet_names()[0]
							 | 
						||
| 
								 | 
							
								  print(f"Reading from sheet {wsname}")
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  # Get data from first sheet
							 | 
						||
| 
								 | 
							
								  ws = wb.get_sheet(wsname)
							 | 
						||
| 
								 | 
							
								  rows = ws.get_rows()
							 | 
						||
| 
								 | 
							
								  for row in rows: print(row)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								if not test_pandas:
							 | 
						||
| 
								 | 
							
								  print("Pandas could not be loaded, skipping tests")
							 | 
						||
| 
								 | 
							
								  exit()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								print("\n## Pandas DataFrame\n")
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								# generate dataframe
							 | 
						||
| 
								 | 
							
								df = pd.DataFrame.from_records(rows)
							 | 
						||
| 
								 | 
							
								print(df.info())
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								outf="pres.xls"
							 | 
						||
| 
								 | 
							
								print(f"\n## Writing to {outf}\n")
							 | 
						||
| 
								 | 
							
								# write JSON string to XLS worksheet
							 | 
						||
| 
								 | 
							
								with SheetJS() as sheetjs:
							 | 
						||
| 
								 | 
							
								  sheetjs.book_from_df(df).to_file(outf)
							 |