forked from sheetjs/docs.sheetjs.com
		
	
		
			
				
	
	
		
			37 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| from sys import stderr, argv;
 | |
| from base64 import b64encode, b64decode;
 | |
| from STPyV8 import JSContext, JSClass;
 | |
| 
 | |
| # Create context with methods for file i/o
 | |
| class Base64Context(JSClass):
 | |
|   def read_file(self, path):
 | |
|     with open(path, "rb") as f:
 | |
|       data = f.read();
 | |
|     return b64encode(data).decode("ascii");
 | |
|   def write_file(self, data, path):
 | |
|     with open(path, "wb") as f:
 | |
|       f.write(b64decode(data));
 | |
| globals = Base64Context();
 | |
| 
 | |
| # Read xlsx.full.min.js
 | |
| with open("xlsx.full.min.js", "r") as f:
 | |
|   sheetjs = f.read();
 | |
| 
 | |
| # The JSContext starts and cleans up the V8 engine
 | |
| with JSContext(globals) as ctxt:
 | |
|   # Load SheetJS library and display version number
 | |
|   ctxt.eval(sheetjs);
 | |
|   version = ctxt.eval("XLSX.version");
 | |
|   print(f"SheetJS Version: {version}", file=stderr);
 | |
| 
 | |
|   # Parse workbook
 | |
|   ctxt.eval(f"globalThis.wb = XLSX.read(read_file('{argv[1]}'), {{type:'base64'}}); void 0;");
 | |
| 
 | |
|   # Print CSV from first worksheet
 | |
|   csv = ctxt.eval("XLSX.utils.sheet_to_csv(wb.Sheets[wb.SheetNames[0]]);");
 | |
|   print(csv);
 | |
| 
 | |
|   # Generate XLSB
 | |
|   xlsb = ctxt.eval("XLSX.write(wb, {type: 'base64', bookType: 'xlsb'})");
 | |
|   globals.write_file(xlsb,"SheetJSSTPyV8.xlsb");
 |