Last active
          March 20, 2021 23:18 
        
      - 
      
 - 
        
Save sergiolucero/b93163a7e86e67dc16203ae8450e3483 to your computer and use it in GitHub Desktop.  
    código para pasar de un PDF del SERVEL (Plebiscito 2020) a formato CSV
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import csv, glob, fitz | |
| def pdf2csv(fn): | |
| csv_fn = fn.replace('.pdf','.csv') | |
| region = int(fn[1:3]) # A04101.pdf -> 4 | |
| with open(csv_fn,'w') as fw: | |
| writer = csv.writer(fw) | |
| writer.writerow(['nombre','rut','genero','direccion', | |
| 'mesa','region','comuna']) | |
| for page in fitz.open(fn): | |
| text = page.getText() | |
| data = text[text.index('de'):].split(chr(10))[14:] | |
| nombres = [n for n in data[::5] if len(n)>1] | |
| ruts = data[1::5] | |
| genedire = data[2::5] | |
| gens = [gd[:5] for gd in genedire] | |
| dirs = [gd[6:] for gd in genedire] | |
| comuna = data[3] | |
| mesas = data[4::5] | |
| zipd = zip(nombres,ruts,gens,dirs,mesas) | |
| for nombre, rut, gene, dire, mesa in zipd: | |
| writer.writerow([nombre,rut,gene,dire,mesa,region,comuna]) | |
| if __name__ == '__main__': | |
| for fn in list(glob.glob('A*.pdf'))[:5]: | |
| print(fn) | |
| pdf2csv(fn) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Aca el parseo solucionado, pero es una solucion muy distinta !
https://github.com/DiegoIdeas/Servel