XML_converter / dn.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 debit_note(inputfile):
if not inputfile.lower().endswith('.csv'):
print('Error: Expected a CSV file')
return None
outputfile = os.path.splitext(inputfile)[0] + '.xml'
try:
df = pd.read_csv(inputfile)
except FileNotFoundError:
print('Error: CSV file not found')
return None
except pd.errors.EmptyDataError:
print('Error: CSV file is empty')
return None
except pd.errors.ParserError:
print('Error: CSV file is malformed or cannot be parsed')
return None
att = df.columns
srop = ''
for j in range(len(df)):
rowop = ''
v_type = ''
cost_centre = ''
cost_category = ''
invoice_num = ''
gstin = ''
date = ''
for i in range(len(att)):
row_value = str(df[att[i]][j]).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace("'", '&apos;').replace('"', '&quot;')
if row_value == 'emp' or att[i].startswith('Mailing'):
continue
if att[i].startswith('VOUCHERTYPENAME'):
v_type = row_value
rowop += f'<{att[i]}>{row_value}</{att[i]}>\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('State_Name'):
if row_value != 'emp':
rowop += f'<STATENAME>{row_value}</STATENAME>\n<BILLTOPLACE>{row_value}</BILLTOPLACE>\n<CONSIGNEESTATENAME>{row_value}</CONSIGNEESTATENAME>\n'
continue
if att[i].startswith('POS'):
rowop += f'<PLACEOFSUPPLY>{row_value}</PLACEOFSUPPLY>\n<SHIPTOPLACE>{row_value}</SHIPTOPLACE>\n'
continue
if att[i].startswith('Registration_Type'):
continue
if att[i].startswith('Company_GSTIN'):
if row_value != 'emp':
rowop += f'<PARTYGSTIN>{row_value}</PARTYGSTIN>\n<CONSIGNEEGSTIN>{row_value}</CONSIGNEEGSTIN>\n'
gstin = row_value
continue
if att[i].startswith('DATE'):
date = row_value
rowop += f'<{att[i]}>{row_value}</{att[i]}>\n<REFERENCEDATE>{date}</REFERENCEDATE>\n'
continue
if att[i].startswith('DebitLedger'):
if row_value != 'emp':
rowop += f'<PARTYNAME>{row_value}</PARTYNAME>\n<PARTYLEDGERNAME>{row_value}</PARTYLEDGERNAME>\n<BASICBUYERNAME>{row_value}</BASICBUYERNAME>\n<ALLLEDGERENTRIES.LIST>\n<LEDGERNAME>{row_value}</LEDGERNAME>\n'
continue
if att[i].startswith('AmountDebit'):
if row_value != 'emp':
rowop += f'<ISDEEMEDPOSITIVE>Yes</ISDEEMEDPOSITIVE>\n<ISPARTYLEDGER>Yes</ISPARTYLEDGER>\n<AMOUNT>{row_value}</AMOUNT>\n<BILLALLOCATIONS.LIST>\n<NAME>{invoice_num}</NAME>\n<BILLTYPE>Agst Ref</BILLTYPE>\n<AMOUNT>{row_value}</AMOUNT>\n</BILLALLOCATIONS.LIST>\n</ALLLEDGERENTRIES.LIST>\n '
continue
if att[i].startswith('CreditLedger'):
if row_value != 'emp':
rowop += f'<ALLLEDGERENTRIES.LIST>\n<LEDGERNAME>{row_value}</LEDGERNAME>\n'
continue
if att[i].startswith('AmountCredit'):
if row_value != 'emp':
if i == len(att) - 1:
rowop += f'<ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE>\n<ISPARTYLEDGER>No</ISPARTYLEDGER>\n<AMOUNT>{row_value}</AMOUNT>\n<CATEGORYALLOCATIONS.LIST>\n<CATEGORY>{cost_category}</CATEGORY>\n<ISDEEMEDPOSITIVE>Yes</ISDEEMEDPOSITIVE>\n<COSTCENTREALLOCATIONS.LIST>\n<NAME>{cost_centre}</NAME>\n<AMOUNT>{row_value}</AMOUNT>\n</COSTCENTREALLOCATIONS.LIST>\n</CATEGORYALLOCATIONS.LIST>\n</ALLLEDGERENTRIES.LIST>\n</VOUCHER>\n</TALLYMESSAGE>\n'
else:
rowop += f'<ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE>\n<ISPARTYLEDGER>No</ISPARTYLEDGER>\n<AMOUNT>{row_value}</AMOUNT>\n<CATEGORYALLOCATIONS.LIST>\n<CATEGORY>{cost_category}</CATEGORY>\n<ISDEEMEDPOSITIVE>Yes</ISDEEMEDPOSITIVE>\n<COSTCENTREALLOCATIONS.LIST>\n<NAME>{cost_centre}</NAME>\n<AMOUNT>{row_value}</AMOUNT>\n</COSTCENTREALLOCATIONS.LIST>\n</CATEGORYALLOCATIONS.LIST>\n</ALLLEDGERENTRIES.LIST>\n '
continue
else:
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"Error writing XML file: {e}")
return None
return outputfile
# Example usage
debit_note('dn.csv')