XML_converter / tally.py
Ninad077's picture
Upload 12 files
1a150a4 verified
import pandas as pd
import os
header = '<ENVELOPE>\n<HEADER>\n<TALLYREQUEST>Import Data</TALLYREQUEST>\n</HEADER>\n<BODY>\n<IMPORTDATA>\n<REQUESTDESC>\n<REPORTNAME>Vouchers</REPORTNAME>\n</REQUESTDESC>\n</IMPORTDATA>\n<REQUESTDATA>\n'
footer = '</REQUESTDATA>\n</BODY>\n</ENVELOPE>\n'
def CSVtoXMLtally(inputfile):
print(f"Processing file: {inputfile}")
if not inputfile.lower().endswith('.csv'):
print('Expected a CSV file')
return None
outputfile = os.path.splitext(inputfile)[0] + '.xml'
try:
df = pd.read_csv(inputfile)
except FileNotFoundError:
print('CSV file not found')
return None
except pd.errors.EmptyDataError:
print('CSV file is empty')
return None
except pd.errors.ParserError:
print('Error parsing CSV file')
return None
att = df.columns
rowop = ''
srop = ''
for j in range(len(df)):
rowop = ''
credit_pass = True
debit_pass = True
cost_category = 'emp'
cost_centre = 'emp'
invoice_num = ''
for i in range(len(att)):
row_value = str(df[att[i]][j])
# Escape special XML characters
row_value = row_value.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace("'", '&apos;').replace('"', '&quot;')
if att[i].startswith('VOUCHERTYPENAME'):
v_type = row_value
rowop += f'<{att[i]}>{row_value}</{att[i]}>\n'
continue
if att[i].startswith('DebitLedger'):
if row_value == 'emp':
continue
rowop += f'<ALLLEDGERENTRIES.LIST>\n<LEDGERNAME>{row_value}</LEDGERNAME>\n'
continue
if att[i].startswith('AmountDebit'):
if row_value == 'emp':
if debit_pass:
rowop += f'</ALLLEDGERENTRIES.LIST>\n'
debit_pass = False
continue
if debit_pass:
rowop += f'<ISDEEMEDPOSITIVE>Yes</ISDEEMEDPOSITIVE>\n<ISPARTYLEDGER>Yes</ISPARTYLEDGER>\n<AMOUNT>{row_value}</AMOUNT>\n'
if cost_centre != 'emp':
rowop += f'<CATEGORYALLOCATIONS.LIST>\n<CATEGORY>{cost_category}</CATEGORY>\n<COSTCENTREALLOCATIONS.LIST>\n<NAME>{cost_centre}</NAME>\n<AMOUNT>{row_value}</AMOUNT>\n</COSTCENTREALLOCATIONS.LIST>\n</CATEGORYALLOCATIONS.LIST>\n'
rowop += '</ALLLEDGERENTRIES.LIST>\n'
debit_pass = False
continue
if att[i].startswith('CreditLedger'):
if row_value == 'emp':
continue
rowop += f'<ALLLEDGERENTRIES.LIST>\n<LEDGERNAME>{row_value}</LEDGERNAME>\n'
continue
if att[i].startswith('AmountCredit'):
if row_value == 'emp':
continue
if credit_pass:
amount = row_value
rowop += f'<ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE>\n<ISPARTYLEDGER>Yes</ISPARTYLEDGER>\n<AMOUNT>{row_value}</AMOUNT>\n<BILLALLOCATIONS.LIST>\n<NAME>{invoice_num}</NAME>\n<BILLTYPE>New Ref</BILLTYPE>\n<AMOUNT>{amount}</AMOUNT>\n</BILLALLOCATIONS.LIST>\n</ALLLEDGERENTRIES.LIST>\n'
credit_pass = False
else:
rowop += f'<ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE>\n<ISPARTYLEDGER>Yes</ISPARTYLEDGER>\n<AMOUNT>{row_value}</AMOUNT>\n</ALLLEDGERENTRIES.LIST>\n'
continue
if att[i].startswith('CostCentre'):
cost_centre = row_value
continue
if att[i].startswith('Invoice'):
invoice_num = row_value
rowop += f'<REFERENCE>{row_value}</REFERENCE>\n'
continue
if att[i].startswith('CostCategory'):
cost_category = row_value
continue
if att[i].startswith('DATE'):
rowop += f'<{att[i]}>{row_value}</{att[i]}>\n'
continue
if att[i].startswith('INVOICEDATE'):
rowop += f'<REFERENCEDATE>{row_value}</REFERENCEDATE>\n'
continue
if att[i].startswith('GSTIN_Number'):
if row_value != 'emp':
rowop += f'<PARTYGSTIN>{row_value}</PARTYGSTIN>\n'
continue
rowop += f'<{att[i]}>{row_value}</{att[i]}>\n'
srop += f'<TALLYMESSAGE>\n<VOUCHER VCHTYPE="{v_type}" ACTION="Create">\n' + rowop
entireop = header + srop + footer
try:
with open(outputfile, 'w+') as f:
f.write(entireop)
print(f"XML file created successfully: {outputfile}")
except Exception as e:
print(f"Failed to write XML file: {e}")
return None
return outputfile