Python Tally Connector - Method 1 - Xml Post Method ElementTree Library
Posted: Tue Apr 15, 2025 5:02 am
Code: Select all
import requests
import xml.etree.ElementTree as ET
# Tally URL (default)
TALLY_URL = "http://localhost:9000"
# XML request to get list of companies
xml_request = """
<ENVELOPE>
<HEADER>
<VERSION>1</VERSION>
<TALLYREQUEST>Export Data</TALLYREQUEST>
<TYPE>Collection</TYPE>
<ID>List of Companies</ID>
</HEADER>
<BODY>
<DESC>
<STATICVARIABLES>
<SVEXPORTFORMAT>$$SysName:XML</SVEXPORTFORMAT>
</STATICVARIABLES>
</DESC>
</BODY>
</ENVELOPE>
"""
# Send the request
response = requests.post(TALLY_URL, data=xml_request)
# Check response
if response.status_code == 200:
try:
root = ET.fromstring(response.text)
print("✅ Connected to Tally. List of Companies:\n")
# Look for all company names in XML
companies = root.findall(".//COMPANY")
if not companies:
print("No companies found in Tally.")
else:
for i, company in enumerate(companies, start=1):
name = company.find("NAME").text if company.find("NAME") is not None else "Unknown"
print(f"{i}. {name}")
except ET.ParseError as e:
print("❌ Failed to parse XML:", e)
else:
print(f"❌ Connection error. Status Code: {response.status_code}")