{"id":20469064,"url":"https://github.com/ghosts6/formatflex","last_synced_at":"2025-03-05T13:20:19.679Z","repository":{"id":209425693,"uuid":"724024815","full_name":"Ghosts6/FormatFlex","owner":"Ghosts6","description":"This repository allows you to convert files between various formats, such as turning PDFs into editable Word documents, XML into visual PDFs, and extracting content from files like Word and XML. It also supports converting Excel (XLSX) to XML and PSD to PNG, with error handling and logging.","archived":false,"fork":false,"pushed_at":"2024-07-20T08:10:47.000Z","size":32,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-16T01:49:22.744Z","etag":null,"topics":["convertor","pdf","pdf-to-word","psd-to-png","python3","xlsx-to-xml"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ghosts6.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-27T08:40:53.000Z","updated_at":"2024-09-14T07:55:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"fd3ccf87-796c-4ba1-bd87-0542db29a968","html_url":"https://github.com/Ghosts6/FormatFlex","commit_stats":null,"previous_names":["ghosts6/convert_pdf_to_wordwithpython"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghosts6%2FFormatFlex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghosts6%2FFormatFlex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghosts6%2FFormatFlex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghosts6%2FFormatFlex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ghosts6","download_url":"https://codeload.github.com/Ghosts6/FormatFlex/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242031611,"owners_count":20060616,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["convertor","pdf","pdf-to-word","psd-to-png","python3","xlsx-to-xml"],"created_at":"2024-11-15T14:07:48.914Z","updated_at":"2025-03-05T13:20:19.645Z","avatar_url":"https://github.com/Ghosts6.png","language":"Python","readme":"![baner](https://github.com/Ghosts6/Local-website/blob/main/img/Baner.png)\n\n# 💻FormatFlex\n\nToday, I'm going to review Python programs that I wrote with the help of Python and its libraries to convert files between various formats. The repository includes several conversion tools, such as:\n\n1. **pdf_to_word**: Utilizes the Aspose.Words library to convert PDF files to Word documents. This method simplifies our work by defining a `convert` function that loads a PDF file and saves it as a Word document.\n\n2. **convert_xlsx_to_xml**: Converts Excel files (XLSX format) to XML format, making it easier to work with Excel data in XML-based systems.\n\n3. **convert.py**: A versatile script that can handle multiple format conversions, including XML, PDF, Word, and TXT.\n\nAdditionally, we have a dedicated program for converting PSD files to PNG format\n\n# 🖥️Convert excel to xml:\n\nHere we have programs that i write with  python to convert excel fille to xml i also create custome one to create xml file which is use for telephone phonebook\nfor this work we write three differend program include:Convert_xlsx_to_xlm_v1.py,Convert_xlsx_to_xlm_v2.py and Convert excel to phonebook.xlm.py\n\n```python\nimport  jpype     \nimport  asposecells     \njpype.startJVM() \nfrom asposecells.api import Workbook\nworkbook = Workbook(\"phonebook01.xlsx\")\nworkbook.save(\"phonebook01.xml\")\njpype.shutdownJVM()\n```\n#convert excel to xml v2:\n\n```python\nfrom openpyxl import load_workbook\nimport xml.etree.ElementTree as ET\n\ndef excel_to_xml(excel_file, xml_file):\n    \n    wb = load_workbook(excel_file)\n    sheet = wb.active\n    \n    # Create the XML \n    root = ET.Element('data')\n    \n    for row in sheet.iter_rows(values_only=True):\n        record = ET.SubElement(root, 'record')\n        for value in row:\n            ET.SubElement(record, 'item').text = str(value) if value is not None else ''\n    \n    tree = ET.ElementTree(root)\n    \n    tree.write(xml_file, encoding='utf-8', xml_declaration=True)\n\nexcel_input_file = 'input.xlsx'\n\nxml_output_file = 'output.xml'\n\n\nexcel_to_xml(excel_input_file, xml_output_file)\n```\n#convert phonebook.xlsx to xml\n\n```python\nimport pandas as pd\nimport xml.etree.ElementTree as ET\n\n# Read the Excel \nexcel_file = 'your_excel_file.xlsx'  # Replace with your Excel file name\ndata = pd.read_excel(excel_file)\n\n# Define updated default values\ndefault_values = {\n    'line': '0',\n    'ring': 'Auto',\n    'group_id_name': 'all contacts',\n    'default_photo': 'Default:default_contact_image.png',\n    'other_number': '', \n    'auto_divert': ''    \n}\n\n# Create the XML structure\nroot = ET.Element('phonebook')\n\n# Iterate through the Excel data and create XML elements\nfor _, row in data.iterrows():\n    contact = ET.SubElement(root, 'contact')\n    for col in ['display_name', 'display_number', 'mobil', 'other_number', 'auto_divert']:\n        value = str(row[col]) if not pd.isnull(row[col]) else default_values.get(col, '')\n        ET.SubElement(contact, col).text = value\n\n# Create the XML\ntree = ET.ElementTree(root)\n\n# Save XML \nxml_output_file = 'phonebook.xml' \ntree.write(xml_output_file, encoding='utf-8', xml_declaration=True)\n```\n\n\n# 📝pdf_to_wordv1.py\n#🚨hint! \n\nthis method use to convert data into image and save it in word file so it keep details but we cant edit it\n\n```python\nimport aspose.words as convert\n# load pdf file\ndoc = convert.Document(\"Input.pdf\") \n# save loaded file as word file\ndoc.save(\"Output.docx\n```\n# 🔧pdf_to_wordv2.py\n\nfor second program we try different way in this method we need to install some python file with help of pip:\n1PyMuPDF 2python-docx\nunlike first program in this one at first we take data from pdf file then we create word file and write data \ninside it,the important difference is with this method we add data as text not image and we can edit them\n\n```python\n#requirement:\n#PyMuPDF\n#python-docx\nimport fitz  \nfrom docx import Document\n\ndef pdf_to_word(pdf_path, word_path):\n\n    pdf_document = fitz.open(pdf_path)\n\n    doc = Document()\n    \n    for page_num in range(pdf_document.page_count):\n        page = pdf_document.load_page(page_num)\n        text = page.get_text(\"text\")\n\n        doc.add_paragraph(text)\n\n    doc.save(word_path)\n    print(f\"PDF converted to Word: {word_path}\")\n\npdf_to_word('input_file.pdf', 'output_file.docx')\n```\n# 📏Convert_program:\n\nWith convert program we can convert different format to each other,only  thing you need some basic lib and some extra like Docx fitz and reportlab after that by running\nprogram and select formats and entering path of input and output we can easliy convert foramts\n\n```python\nimport fitz\nimport docx  \nfrom docx import Document\nfrom reportlab.lib.pagesizes import letter\nfrom reportlab.pdfgen import canvas\nfrom xml.etree import ElementTree as ET\n\ndef pdf_to_word(pdf_path, word_path):\n    pdf_document = fitz.open(pdf_path)\n    doc = Document()\n    \n    for page_num in range(pdf_document.page_count):\n        page = pdf_document.load_page(page_num)\n        text = page.get_text(\"text\")\n        doc.add_paragraph(text)\n\n    doc.save(word_path)\n    print(f\"PDF converted to Word: {word_path}\")\n\ndef xml_to_pdf(file_path, file_2_path):\n    with open(file_path, 'r') as xml_file:\n        xml_data = xml_file.read()\n    \n    root = ET.fromstring(xml_data)\n    \n    c = canvas.Canvas(file_2_path, pagesize=letter)\n    \n    y_offset = 750\n    for element in root:\n        if element.tag == 'title':\n            c.setFont(\"Helvetica-Bold\", 16)\n            c.drawString(100, y_offset, element.text)\n            y_offset -= 20\n            \n        elif element.tag == 'paragraph':\n            c.setFont(\"Helvetica\", 12)\n            c.drawString(100, y_offset, element.text)\n            y_offset -= 15\n            \n    c.save()\n    print(f\"XML converted to PDF: {file_2_path}\")\n\ndef xml_to_pdf(file_path, file_2_path):\n    with open(file_path, 'r') as xml_file:\n        xml_data = xml_file.read()\n    \n    root = ET.fromstring(xml_data)\n    \n    c = canvas.Canvas(file_2_path, pagesize=letter)\n    \n    y_offset = 750\n    for element in root:\n        if element.tag == 'title':\n            c.setFont(\"Helvetica-Bold\", 16)\n            c.drawString(100, y_offset, element.text)\n            y_offset -= 20\n            \n        elif element.tag == 'paragraph':\n            c.setFont(\"Helvetica\", 12)\n            c.drawString(100, y_offset, element.text)\n            y_offset -= 15\n            \n    c.save()\n    print(f\"XML converted to PDF: {file_2_path}\")\n\ndef xml_to_word(file_path, file_2_path):\n    with open(file_path, 'r') as xml_file:\n        xml_data = xml_file.read()\n    \n    root = ET.fromstring(xml_data)\n    \n    doc = Document()\n    \n    for element in root:\n        if element.tag == 'title':\n            doc.add_heading(element.text, level=1)\n            \n        elif element.tag == 'paragraph':\n            doc.add_paragraph(element.text)\n    \n    doc.save(file_2_path)\n    print(f\"XML converted to Word: {file_2_path}\")\n\ndef xml_to_txt(file_path, file_2_path):\n    with open(file_path, 'r') as xml_file:\n        xml_data = xml_file.read()\n    \n    root = ET.fromstring(xml_data)\n    \n    with open(file_2_path, 'w') as txt_file:\n        for element in root:\n            txt_file.write(element.text + '\\n')\n    \n    print(f\"XML converted to TXT: {file_2_path}\")\n\ndef pdf_to_xml(pdf_path, xml_path):\n    pdf_document = fitz.open(pdf_path)\n    \n    root = ET.Element(\"document\")\n    \n    for page_num in range(pdf_document.page_count):\n        page = pdf_document.load_page(page_num)\n        text = page.get_text()\n        \n        page_element = ET.SubElement(root, \"page\")\n        \n        page_element.text = text\n    \n    tree = ET.ElementTree(root)\n    tree.write(xml_path)\n    \n    print(f\"PDF converted to XML: {xml_path}\")\n\ndef pdf_to_txt(pdf_path, txt_path):\n    pdf_document = fitz.open(pdf_path)\n    text = \"\"\n    \n    for page_num in range(pdf_document.page_count):\n        page = pdf_document.load_page(page_num)\n        text += page.get_text()\n    \n    with open(txt_path, 'w') as txt_file:\n        txt_file.write(text)\n    \n    print(f\"PDF converted to TXT: {txt_path}\")\n\ndef word_to_xml(word_path, xml_path):\n    doc = docx.Document(word_path)\n    \n    root = ET.Element(\"document\")\n    \n    for paragraph in doc.paragraphs:\n        paragraph_element = ET.SubElement(root, \"paragraph\")\n\n        paragraph_element.text = paragraph.text\n\n    tree = ET.ElementTree(root)\n    tree.write(xml_path)\n    \n    print(f\"Word converted to XML: {xml_path}\")\n\ndef word_to_pdf(file_path, file_2_path):\n    doc = docx.Document(file_path)\n    c = canvas.Canvas(file_2_path, pagesize=letter)\n    \n    for paragraph in doc.paragraphs:\n        c.drawString(100, 750, paragraph.text)\n        c.showPage()\n    \n    c.save()\n    \n    print(f\"Word converted to PDF: {file_2_path}\")\n\ndef word_to_txt(file_path, file_2_path):\n    doc = docx.Document(file_path)\n    \n    with open(file_2_path, 'w') as txt_file:\n        for paragraph in doc.paragraphs:\n            txt_file.write(paragraph.text + '\\n')\n    \n    print(f\"Word converted to TXT: {file_2_path}\")\n\ndef txt_to_xml(file_path, file_2_path):\n    with open(file_path, 'r') as txt_file:\n        txt_data = txt_file.read()\n    \n    root = ET.Element(\"document\")\n    \n    for line in txt_data.split('\\n'):\n        paragraph_element = ET.SubElement(root, \"paragraph\")\n        paragraph_element.text = line\n    \n    tree = ET.ElementTree(root)\n    tree.write(file_2_path)\n    \n    print(f\"TXT converted to XML: {file_2_path}\")\n\ndef txt_to_pdf(file_path, file_2_path):\n    with open(file_path, 'r') as txt_file:\n        txt_data = txt_file.read()\n    \n    c = canvas.Canvas(file_2_path, pagesize=letter)\n    c.drawString(100, 750, txt_data)\n    c.save()\n    \n    print(f\"TXT converted to PDF: {file_2_path}\")\n\ndef txt_to_word(file_path, file_2_path):\n    doc = docx.Document()\n    \n    with open(file_path, 'r') as txt_file:\n        txt_data = txt_file.read()\n    \n    for line in txt_data.split('\\n'):\n        doc.add_paragraph(line)\n    \n    doc.save(file_2_path)\n    \n    print(f\"TXT converted to Word: {file_2_path}\")\n\nprint(\"---Welcome to our conversion program---\")\nchoice = input(\"Select your input file type (xml/pdf/word/txt): \")\nconvert = input(\"Select the output file type (xml/pdf/word/txt): \")\nchoice_path = input(\"Please enter the path of the input file: \")\nconvert_path = input(\"Please enter the path of the output file: \")\n\nif choice == \"xml\":\n    if convert == \"pdf\":\n        xml_to_pdf(choice_path, convert_path)\n    elif convert == \"word\":\n        xml_to_word(choice_path, convert_path)\n    elif convert == \"txt\":\n        xml_to_txt(choice_path, convert_path)\n    else:\n        print(\"Wrong type or same type as input file, please try again.\")\n\nelif choice == \"pdf\":\n    if convert == \"xml\":\n        pdf_to_xml(choice_path, convert_path)\n    elif convert == \"word\":\n        pdf_to_word(choice_path, convert_path)\n    elif convert == \"txt\":\n        pdf_to_txt(choice_path, convert_path)\n    else:\n        print(\"Wrong type or same type as input file, please try again.\")\n\nelif choice == \"word\":\n    if convert == \"pdf\":\n        word_to_pdf(choice_path, convert_path)\n    elif convert == \"xml\":\n        word_to_xml(choice_path, convert_path)\n    elif convert == \"txt\":\n        word_to_txt(choice_path, convert_path)\n    else:\n        print(\"Wrong type or same type as input file, please try again.\")\n\nelif choice == \"txt\":\n    if convert == \"xml\":\n        txt_to_xml(choice_path, convert_path)\n    elif convert == \"pdf\":\n        txt_to_pdf(choice_path, convert_path)\n    elif convert == \"word\":\n        txt_to_word(choice_path, convert_path)\n    else:\n        print(\"Wrong type or same type as input file, please try again.\")\nelse:\n    print(\"Invalid input file type. Please select xml/pdf/word/txt.\")\n```\n# 🖼️ PSD to PNG Conversion Program\n\nThis program converts PSD files to PNG format with ease. It traverses directories, converts all PSD files it finds, and saves the converted PNG files in a specified output directory. The program also includes robust error handling and logging to ensure smooth operation even when encountering problematic files.\n\n## ✨ Features\n\n- 🚀 **Automated Conversion:** Convert all PSD files in a directory and its subdirectories to PNG format.\n- 📂 **Directory Handling:** Organizes converted PNG files in the specified output directory, mirroring the original directory structure.\n- 🛠️ **Error Handling:** Continues processing remaining files even if some conversions fail.\n- 📄 **Logging:** Logs errors and successful conversions to a `log.txt` file in the output directory for easy troubleshooting.\n\n## 🛠️ Requirements\n\nMake sure you have the following Python libraries installed:\n- `Pillow`\n- `psd_tools`\n\nYou can install them using pip:\n```sh\npip install pillow psd-tools\n```\n\n## 📝 Example Code\n```python\nfrom PIL import Image\nimport psd_tools\nimport os\nimport logging\n\n# logging\ndef setup_logging(output_base_directory):\n    log_path = os.path.join(output_base_directory, 'log.txt')\n    logging.basicConfig(filename=log_path, level=logging.DEBUG, \n                        format='%(asctime)s - %(levelname)s - %(message)s')\n\ndef convert_psd_to_png(directory, output_base_directory):\n    for root, _, files in os.walk(directory):\n        for filename in files:\n            if filename.lower().endswith('.psd'):\n                file_path = os.path.join(root, filename)\n                try:\n                    psd = psd_tools.PSDImage.open(file_path)\n                    composite = psd.compose()\n\n                    relative_path = os.path.relpath(root, directory)\n                    output_directory = os.path.join(output_base_directory, relative_path)\n                    os.makedirs(output_directory, exist_ok=True)\n\n                    png_path = os.path.join(output_directory, os.path.splitext(filename)[0] + '.png')\n                    composite.save(png_path)\n\n                    print(f\"Converted {filename} to {png_path}\")\n                    logging.info(f\"Successfully converted {file_path} to {png_path}\")\n                except Exception as e:\n                    print(f\"Error converting {filename}: {e}\")\n                    logging.error(f\"Failed to convert {file_path}: {e}\")\n\ndef main():\n    while True:\n        directory = input(\"Enter the base directory path containing PSD files: \")\n        output_base_directory = os.path.join(directory, 'png')\n        os.makedirs(output_base_directory, exist_ok=True)\n        \n        setup_logging(output_base_directory)\n\n        convert_psd_to_png(directory, output_base_directory)\n\n        continue_choice = input(\"Do you want to convert files in another directory? (yes/no): \").strip().lower()\n        if continue_choice != 'yes':\n            break\n\nif __name__ == \"__main__\":\n    main()\n```\n# 🚀 Usage\nClone the Repository:\n```sh\ngit clone https://github.com/yourusername/psd-to-png-converter.git\ncd psd-to-png-converter\n```\nRun the Program:\n```sh\n#example\npython3 psd_to_png.py\n```\nFollow the Prompts:\n\n# 📬 Contact\nFor any questions or suggestions, please open an issue or contact us at kiarash@kiarashbashokian.com \n\n# Happy converting! 🎉","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghosts6%2Fformatflex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fghosts6%2Fformatflex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghosts6%2Fformatflex/lists"}