Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
calcolo-tasse-bustepaghe.py
#!/usr/bin/env python3
from io import StringIO
from termcolor import colored
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
import sys
import os
if len(sys.argv) < 3:
cedolinipdf = [filename for filename in os.listdir(sys.argv[1]) if filename.startswith("CEDOLINI")]
modf24pdf = [filename for filename in os.listdir(sys.argv[1]) if filename.startswith("MOD.F24")]
if cedolinipdf == '' and modf24pdf == '':
print('Primo parametro Cedolini e poi MOD F24')
sys.exit()
else:
cedolinipdf = sys.argv[1] + cedolinipdf[0]
modf24pdf = sys.argv[1] + modf24pdf[0]
print('File trovati!')
else:
cedolinipdf = sys.argv[1]
modf24pdf = sys.argv[2]
# Extract the content from PDF
output_string = StringIO()
with open(cedolinipdf, 'rb') as in_file:
parser = PDFParser(in_file)
doc = PDFDocument(parser)
rsrcmgr = PDFResourceManager()
device = TextConverter(rsrcmgr, output_string, laparams=LAParams())
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.create_pages(doc):
interpreter.process_page(page)
# I don't know why the output is on this variable
output_string = output_string.getvalue().splitlines()
next_is_netto = False
next_3_lines_competenze = -1
netto = []
competenze = []
for line in output_string:
if next_is_netto and line != '':
netto.append(float(line.strip().replace('.', '').replace(',','.')))
next_is_netto = False
if next_3_lines_competenze > 0:
next_3_lines_competenze = next_3_lines_competenze -1
if next_3_lines_competenze == 0:
competenze.append(float(line.strip().replace('.', '').replace(',','.')))
next_3_lines_competenze = -1
if line == 'N E T T O DEL M E S E':
next_is_netto = True
if line.strip() == 'Imp. INPS Imp. INAIL Imp. IRPEF IRPEF pagata':
next_3_lines_competenze = 3
# Extract the content from PDF
output_string = StringIO()
with open(modf24pdf, 'rb') as in_file:
parser = PDFParser(in_file)
doc = PDFDocument(parser)
rsrcmgr = PDFResourceManager()
device = TextConverter(rsrcmgr, output_string, laparams=LAParams())
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.create_pages(doc):
interpreter.process_page(page)
output_string = output_string.getvalue().splitlines()
saldo = ''
for line in output_string:
if '.' in line and line[0].isdigit():
saldo = float(line.strip().replace(' ', ',').replace('.', '').replace(',','.'))
if 'ON-LINE INTERMED Scad.' in line:
print('Scadenza: ' + line.strip().replace('ON-LINE INTERMED Scad.','').replace(' ', '/'))
primo = netto[0] + netto[1] + saldo
secondo = competenze[0] + competenze[1]
dolce = primo - secondo
print('Netto: ' + str(netto))
print('Competenze: ' + str(competenze))
print('Saldo F24: ' + str(saldo))
print('Somma netti e saldo finale: ' + str(round(primo,2)))
print('Tasse: ' + str(round(dolce,2)))
print('Importo Netto: ' + str(netto[0] + netto[1]).replace('.', ','))
print('Importo Lordo: ' + str(secondo).replace('.', ','))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment