XML_converter / jv.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>All Masters</REPORTNAME>\n</REQUESTDESC>\n<REQUESTDATA>\n'
footer = '</REQUESTDATA>\n</IMPORTDATA>\n</BODY>\n</ENVELOPE>\n'
def journal_vouchers(inputfile):
if not inputfile.lower().endswith('.csv'):
raise ValueError('Expected a CSV File')
outputfile = os.path.splitext(inputfile)[0] + '.xml'
try:
df = pd.read_csv(inputfile)
except FileNotFoundError:
raise FileNotFoundError('CSV file not found')
except Exception as e:
raise Exception(f'Error reading CSV file: {e}')
att = df.columns
rowop = ''
srop = ''
v_type = '' # Initialize v_type to avoid UnboundLocalError
for j in range(len(df)):
rowop = ''
for i in range(len(att)):
row_value = str(df[att[i]][j])
row_value = row_value.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace("'", '&apos;').replace('"', '&quot;')
if att[i].startswith('VOUCHERNUMBER') or att[i].startswith('Mode'):
continue
if att[i].startswith('VOUCHERTYPENAME'):
v_type = row_value
party_name = df[att[i+4]][j]
party_name = party_name.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace("'", '&apos;').replace('"', '&quot;')
rowop += f'<PARTYLEDGERNAME>{party_name}</PARTYLEDGERNAME>\n<{att[i]}>{row_value}</{att[i]}>\n<REFERENCE>On Account</REFERENCE>\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':
continue
rowop += f'<ISDEEMEDPOSITIVE>Yes</ISDEEMEDPOSITIVE>\n<AMOUNT>{row_value}</AMOUNT>\n</ALLLEDGERENTRIES.LIST>\n'
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' and i == len(att) - 1:
rowop += f'</VOUCHER>\n</TALLYMESSAGE>\n'
continue
elif row_value == 'emp':
continue
elif i == len(att) - 1:
rowop += f'<ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE>\n<AMOUNT>{row_value}</AMOUNT>\n</ALLLEDGERENTRIES.LIST>\n</VOUCHER>\n</TALLYMESSAGE>\n'
continue
else:
rowop += f'<ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE>\n<AMOUNT>{row_value}</AMOUNT>\n</ALLLEDGERENTRIES.LIST>\n'
if att[i].startswith('DATE'):
rowop += f'<{att[i]}>{row_value}</{att[i]}>\n'
continue
else:
rowop += f'<{att[i]}>{row_value}</{att[i]}>\n'
srop += f'<TALLYMESSAGE xmlns:UDF="TallyUDF">\n<VOUCHER VCHTYPE="{v_type}" ACTION="Create" OBJVIEW="Accounting Voucher View">\n' + rowop
entireop = header + srop + footer
try:
with open(outputfile, 'w+') as f:
f.write(entireop)
except Exception as e:
raise Exception(f'Error writing XML file: {e}')
# Debug print
print(f'XML file created at: {outputfile}')
if os.path.exists(outputfile):
return outputfile
else:
raise Exception('Failed to create XML file.')