Skip to content

Instantly share code, notes, and snippets.

@tejavarma-aln
Last active May 28, 2023 13:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tejavarma-aln/306db4fbbd33465130c23ce3061d0011 to your computer and use it in GitHub Desktop.
Save tejavarma-aln/306db4fbbd33465130c23ce3061d0011 to your computer and use it in GitHub Desktop.
Fetch Ledgers with python from tally
<ENVELOPE>
<HEADER>
<VERSION>1</VERSION>
<TALLYREQUEST>EXPORT</TALLYREQUEST>
<TYPE>COLLECTION</TYPE>
<ID>List of Ledgers</ID>
</HEADER>
<BODY>
<DESC>
<STATICVARIABLES>
<SVEXPORTFORMAT>$$SysName:XML</SVEXPORTFORMAT>
</STATICVARIABLES>
</DESC>
</BODY>
</ENVELOPE>
#import request package
#import package to parse xml response
import xml.etree.ElementTree as Et
#tally ip and port
url = "http://localhost:9000"
#XML body to send - Payload
data = "<ENVELOPE><HEADER><VERSION>1</VERSION><TALLYREQUEST>EXPORT</TALLYREQUEST><TYPE>COLLECTION</TYPE><ID>List of Ledgers</ID>"
data += "</HEADER><BODY><DESC><STATICVARIABLES><SVEXPORTFORMAT>$$SysName:XML</SVEXPORTFORMAT></STATICVARIABLES></DESC></BODY></ENVELOPE>"
#make request
request = requests.post(url = url,data=data)
#parse response
response = request.text.strip().replace("&amp;","and") #replace special char &
responseXML = Et.fromstring(response)
#loop through xmlnodes and get ledger names using Xpath
for data in responseXML.findall('./BODY/DATA/COLLECTION/LEDGER'):
print(data.get('NAME'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment