Spaces:
Sleeping
Sleeping
import json | |
import pandas as pd | |
import dicttoxml | |
from pathlib import Path | |
from typing import Dict, Any, List | |
from .config import settings | |
class OutputFormatter: | |
"""Handles conversion of invoice data to different formats (JSON, XML, CSV, Excel).""" | |
def _flatten_data(self, json_data: Dict[str, Any]) -> pd.DataFrame: | |
"""Flatten the JSON data into a pandas DataFrame.""" | |
df = pd.DataFrame(json_data) | |
print(df) | |
return df | |
def _create_dataframe(self, json_data: Dict[str, Any]) -> pd.DataFrame: | |
"""Convert JSON data to pandas DataFrame.""" | |
return self._flatten_data(json_data) | |
def save_as_json(self, data: Dict[str, Any], output_path: Path) -> str: | |
"""Save data as JSON file.""" | |
output_file = output_path.with_suffix('.json') | |
with open(output_file, 'w', encoding='utf-8') as f: | |
json.dump(data, f, indent=2, ensure_ascii=False) | |
return str(output_file) | |
def save_as_xml(self, data: Dict[str, Any], output_path: Path) -> str: | |
"""Save data as XML file.""" | |
output_file = output_path.with_suffix('.xml') | |
# Convert dict to XML with custom root element | |
xml_data = dicttoxml.dicttoxml(data, custom_root='invoice', attr_type=False) | |
with open(output_file, 'wb') as f: | |
f.write(xml_data) | |
return str(output_file) | |
def save_as_csv(self, data: Dict[str, Any], output_path: Path) -> str: | |
"""Save data as CSV file with flattened structure.""" | |
output_file = output_path.with_suffix('.csv') | |
df = self._create_dataframe(data) | |
# Save to CSV with UTF-8 encoding | |
df.to_csv(output_file, index=False, encoding='utf-8') | |
return str(output_file) | |
def save_as_excel(self, data: Dict[str, Any], output_path: Path) -> str: | |
"""Save data as Excel file with formatted structure.""" | |
output_file = output_path.with_suffix('.xlsx') | |
df = self._create_dataframe(data) | |
df.to_excel(output_file, index=False) | |
return str(output_file) | |
def save_all_formats(self, data: Dict[str, Any], base_path: Path) -> Dict[str, str]: | |
"""Save data in all available formats and return paths.""" | |
return { | |
'json': self.save_as_json(data, base_path), | |
'xml': self.save_as_xml(data, base_path), | |
'csv': self.save_as_csv(data, base_path), | |
'excel': self.save_as_excel(data, base_path) | |
} |