How to read all SVG file content as plain text?

143 Views Asked by At

currently I'm trying to read all SVG files' content in order to ultimately merge it into one svg file after some modification. Some icon sample looks like this:

<svg width="26" height="26" viewBox="0 0 26 26" fill="none" xmlns="http://www.w3.org/2000/svg">
<mask id="path-1-outside-1_420_1264" maskUnits="userSpaceOnUse" x="4" y="5" width="16" height="15" fill="black">
<rect fill="white" x="4" y="5" width="16" height="15"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M17.1331 11.529C18.2573 11.1965 19.0666 10.1065 18.9957 8.83631C18.92 7.47453 17.8082 6.38164 16.4694 6.3508C15.801 6.33538 15.1892 6.57729 14.7212 6.98688H14.5624C14.0509 6.38261 13.2936 6 12.4484 6C11.6031 6 10.8458 6.38261 10.3343 6.98688H10.2785C9.75379 6.52717 9.04659 6.27852 8.2836 6.36815C7.12163 6.505 6.179 7.44466 6.02394 8.62718C5.84809 9.96775 6.66024 11.1483 7.8241 11.5165L8.91989 17.8955C9.02862 18.5345 9.57321 19 10.2086 19H14.7515C15.3869 19 15.9305 18.5335 16.0402 17.8955L17.1331 11.529ZM13.0642 14.7386C13.0642 14.4624 12.8403 14.2386 12.5642 14.2386C12.288 14.2386 12.0642 14.4624 12.0642 14.7386V17.7153C12.0642 17.9915 12.288 18.2153 12.5642 18.2153C12.8403 18.2153 13.0642 17.9915 13.0642 17.7153V14.7386ZM14.9979 14.296C15.2661 14.3614 15.4306 14.632 15.3651 14.9002L14.6595 17.7917C14.594 18.06 14.3235 18.2244 14.0552 18.1589C13.7869 18.0934 13.6225 17.8229 13.688 17.5546L14.3936 14.6632C14.4591 14.3949 14.7296 14.2305 14.9979 14.296ZM10.819 14.663C10.7534 14.3948 10.4828 14.2304 10.2146 14.296C9.94631 14.3615 9.78199 14.6321 9.84754 14.9004L10.5541 17.7918C10.6197 18.0601 10.8903 18.2244 11.1585 18.1589C11.4268 18.0933 11.5911 17.8227 11.5255 17.5545L10.819 14.663Z"/>
</mask>
<path d="M18.9957 8.83631L17.9972 8.89177L17.9972 8.89205L18.9957 8.83631ZM17.1331 11.529L16.8495 10.5701L16.2528 10.7466L16.1475 11.3598L17.1331 11.529ZM16.4694 6.3508L16.4463 7.35054L16.4464 7.35054L16.4694 6.3508ZM14.7212 6.98688V7.98688H15.097L15.3798 7.73938L14.7212 6.98688ZM14.5624 6.98688L13.7991 7.63296L14.0987 7.98688H14.5624V6.98688ZM10.3343 6.98688V7.98688H10.798L11.0976 7.63296L10.3343 6.98688ZM10.2785 6.98688L9.61956 7.73905L9.90244 7.98688H10.2785V6.98688ZM8.2836 6.36815L8.16693 5.37498L8.16663 5.37502L8.2836 6.36815ZM6.02394 8.62718L7.01545 8.75724L7.01546 8.75719L6.02394 8.62718ZM7.8241 11.5165L8.80967 11.3472L8.70653 10.7468L8.12569 10.5631L7.8241 11.5165ZM8.91989 17.8955L9.90572 17.7278L9.90546 17.7262L8.91989 17.8955ZM16.0402 17.8955L17.0257 18.065L17.0257 18.0647L16.0402 17.8955ZM15.3651 14.9002L14.3936 14.6632L14.3936 14.6632L15.3651 14.9002ZM14.9979 14.296L14.7608 15.2675L14.7608 15.2675L14.9979 14.296ZM14.6595 17.7917L15.631 18.0288L15.631 18.0288L14.6595 17.7917ZM14.0552 18.1589L14.2923 17.1874L14.2923 17.1874L14.0552 18.1589ZM13.688 17.5546L12.7165 17.3175V17.3175L13.688 17.5546ZM14.3936 14.6632L15.3651 14.9002V14.9002L14.3936 14.6632ZM10.2146 14.296L10.4519 15.2674L10.4519 15.2674L10.2146 14.296ZM10.819 14.663L11.7904 14.4256L10.819 14.663ZM9.84754 14.9004L8.87613 15.1378L9.84754 14.9004ZM10.5541 17.7918L11.5255 17.5545H11.5255L10.5541 17.7918ZM11.1585 18.1589L10.9211 17.1874L10.9211 17.1874L11.1585 18.1589ZM11.5255 17.5545L10.5541 17.7918L11.5255 17.5545ZM17.9972 8.89205C18.0422 9.69711 17.5309 10.3685 16.8495 10.5701L17.4167 12.488C18.9836 12.0245 20.091 10.516 19.9941 8.78057L17.9972 8.89205ZM16.4464 7.35054C17.2491 7.36903 17.9497 8.03658 17.9972 8.89177L19.9941 8.78085C19.8904 6.91249 18.3673 5.39426 16.4924 5.35107L16.4464 7.35054ZM15.3798 7.73938C15.6685 7.48673 16.0397 7.34116 16.4463 7.35054L16.4925 5.35107C15.5622 5.32961 14.71 5.66784 14.0627 6.23437L15.3798 7.73938ZM14.5624 7.98688H14.7212V5.98688H14.5624V7.98688ZM12.4484 7C12.9837 7 13.4672 7.24084 13.7991 7.63296L15.3257 6.3408C14.6346 5.52438 13.6035 5 12.4484 5V7ZM11.0976 7.63296C11.4295 7.24084 11.913 7 12.4484 7V5C11.2933 5 10.2621 5.52438 9.57104 6.3408L11.0976 7.63296ZM10.2785 7.98688H10.3343V5.98688H10.2785V7.98688ZM8.40027 7.36132C8.86885 7.30628 9.29726 7.4567 9.61956 7.73905L10.9375 6.23471C10.2103 5.59764 9.22432 5.25077 8.16693 5.37498L8.40027 7.36132ZM7.01546 8.75719C7.11231 8.01856 7.70338 7.4434 8.40057 7.36129L8.16663 5.37502C6.53987 5.56661 5.24569 6.87076 5.03243 8.49717L7.01546 8.75719ZM8.12569 10.5631C7.41799 10.3392 6.90347 9.61092 7.01545 8.75724L5.03244 8.49711C4.79271 10.3246 5.90249 11.9575 7.52251 12.4699L8.12569 10.5631ZM9.90546 17.7262L8.80967 11.3472L6.83854 11.6858L7.93433 18.0648L9.90546 17.7262ZM10.2086 18C10.073 18 9.93474 17.8983 9.90572 17.7278L7.93406 18.0633C8.12251 19.1707 9.07339 20 10.2086 20V18ZM14.7515 18H10.2086V20H14.7515V18ZM15.0546 17.7261C15.0249 17.8989 14.8853 18 14.7515 18V20C15.8884 20 16.8361 19.1682 17.0257 18.065L15.0546 17.7261ZM16.1475 11.3598L15.0546 17.7263L17.0257 18.0647L18.1187 11.6982L16.1475 11.3598ZM12.5642 15.2386C12.288 15.2386 12.0642 15.0147 12.0642 14.7386H14.0642C14.0642 13.9101 13.3926 13.2386 12.5642 13.2386V15.2386ZM13.0642 14.7386C13.0642 15.0147 12.8403 15.2386 12.5642 15.2386V13.2386C11.7357 13.2386 11.0642 13.9101 11.0642 14.7386H13.0642ZM13.0642 17.7153V14.7386H11.0642V17.7153H13.0642ZM12.5642 17.2153C12.8403 17.2153 13.0642 17.4392 13.0642 17.7153H11.0642C11.0642 18.5437 11.7357 19.2153 12.5642 19.2153V17.2153ZM12.0642 17.7153C12.0642 17.4392 12.288 17.2153 12.5642 17.2153V19.2153C13.3926 19.2153 14.0642 18.5437 14.0642 17.7153H12.0642ZM12.0642 14.7386V17.7153H14.0642V14.7386H12.0642ZM16.3366 15.1373C16.533 14.3325 16.0398 13.5209 15.2349 13.3245L14.7608 15.2675C14.4925 15.202 14.3281 14.9314 14.3936 14.6632L16.3366 15.1373ZM15.631 18.0288L16.3366 15.1373L14.3936 14.6632L13.688 17.5546L15.631 18.0288ZM13.8181 19.1304C14.6229 19.3268 15.4346 18.8336 15.631 18.0288L13.688 17.5546C13.7535 17.2863 14.024 17.1219 14.2923 17.1874L13.8181 19.1304ZM12.7165 17.3175C12.5201 18.1223 13.0133 18.934 13.8181 19.1304L14.2923 17.1874C14.5605 17.2529 14.7249 17.5234 14.6595 17.7917L12.7165 17.3175ZM13.4221 14.4261L12.7165 17.3175L14.6595 17.7917L15.3651 14.9002L13.4221 14.4261ZM15.235 13.3245C14.4301 13.1281 13.6185 13.6213 13.4221 14.4261L15.3651 14.9002C15.2996 15.1685 15.0291 15.3329 14.7608 15.2675L15.235 13.3245ZM10.4519 15.2674C10.1837 15.333 9.91309 15.1686 9.84754 14.9004L11.7904 14.4256C11.5937 13.6209 10.7819 13.1279 9.97718 13.3246L10.4519 15.2674ZM10.819 14.663C10.8845 14.9313 10.7202 15.2019 10.4519 15.2674L9.97719 13.3246C9.17244 13.5212 8.67948 14.333 8.87613 15.1378L10.819 14.663ZM11.5255 17.5545L10.819 14.663L8.87613 15.1378L9.58269 18.0292L11.5255 17.5545ZM10.9211 17.1874C11.1894 17.1219 11.46 17.2862 11.5255 17.5545L9.58269 18.0292C9.77934 18.834 10.5911 19.3269 11.3959 19.1303L10.9211 17.1874ZM10.5541 17.7918C10.4886 17.5236 10.6529 17.253 10.9211 17.1874L11.3959 19.1303C12.2006 18.9336 12.6936 18.1218 12.4969 17.3171L10.5541 17.7918ZM9.84754 14.9004L10.5541 17.7918L12.4969 17.3171L11.7904 14.4256L9.84754 14.9004Z" fill="#1B1A1A" mask="url(#path-1-outside-1_420_1264)"/>
</svg>

I am using the following code with the use of minidom to parse the SVG file content and print them into plain text. However the current function doesn't work as intended. Is there a problem with how I defined the get_text_content()?

import csv
import os, stat
path = "./data/Icons/"

def get_text_content(node):
    text_content = ""
    if node.nodeType == node.TEXT_NODE:
        text_content += node.data
    elif node.nodeType == node.ELEMENT_NODE:
        for child_node in node.childNodes:
            text_content += get_text_content(child_node)
    return text_content

for file in os.listdir(path):
    print(file)
    svg_file = os.path.join(path + file)
    os.chmod(svg_file, stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO)
    doc = minidom.parse(svg_file)
    plain_text = get_text_content(doc.documentElement)
    print(plain_text)
    doc.unlink()
0

There are 0 best solutions below