I extracted svg from html using Python 3 and BeatifulSoup 4.Please tell me how to save it as an svg file in order to paste it into Excel as an svg image.

Asked 2 years ago, Updated 2 years ago, 451 views

soup=BeautifulSoup(html, "html.parser")
svgs=soup.findAll('svg')
I want to save as svgs[1]#<-----.svg 

python python3 beautifulsoup svg

2022-09-30 22:00

2 Answers

The following is a list of
to save to the desktop under the name temp.svg (Assume that the name of the desktop folder is Desktop (if you haven't renamed it, it should be fine))

 from bs4 import BeautifulSoup
from pathlib import Path

soup = BeautifulSoup(text, 'html.parser')
svgs=soup('svg')

desktop=Path.home()/'Desktop'
if desktop.exists():
    with(desktop/'temp.svg') .open('w') as fp:
        fp.write(str(svgs[1]))


2022-09-30 22:00

@oriri To supplement your answer, the .svg file is XML-formatted data written in text.

SVG:Scalable Vector Graphics

SVG images and associated behaviors are defined in XML text files so that you can search, index, script, and compress them.In addition, this means that you can create and edit any text editor or draw software.

Scalable Vector Graphics-Wikipedia

Scalable Vector Graphics (SVG, Day: Variable Vector Graphics [2][3]) is an XML-based image format for two-dimensional vector images.

File Format
Basically, SVG is an image format specified by image/svg+xml in MIME format specification.The file extensions are .svg and gzip compressed .svgz.Because the extension .svg is a text file, the disadvantage of large data in communication traffic between networks is large, but the compressed .svgz is a fraction of the file size.Deployment is handled by the web browser.

Example-Scalable Vector Graphics-Wikipedia

Example
 Skip the diagram

<?xml version="1.0" encoding="UTF-8" standardone="no"?>
<!DOCTYPE svg PUBLIC"-//W3C//DTD SVG 1.1//EN"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="391" height="391" viewBox="-70.5-70.5391 391"xmlns="http://www.w3.org/2000/svg"xmlns:xlink="http://www.w3.org/1999/xlink">
<rect fill="#fff" stroke="#000" x="-70"y="-70" width="390" height="390" />
<gopacity="0.8">
  <rectx="25"y="25" width="200" height="200" fill="lime" stroke-width="4" stroke="pink"/>
  <circle cx="125"cy="125"r="75" fill="orange"/>
  <polyline points="50,15050,200200,200200,100" stroke="red" stroke-width="4" fill="none"/>
  <line x1="50" y1="50" x2="200" y2="200" stroke="blue" stroke-width="4"/gt;
</g>
</svg>

W3C Sample File Folder
Index of/SVG/tools/svgweb/samples/svg-files

If you look at some of the sample files above, you can see that they start with <?xml and start with <svg, but end with </svg> text files.

SVG data as a web page probably starts with <svg and ends with </svg>, but if you write it as a file with the extension .svg, it will be an SVG file.

A simple way to do that will be @oriri's answer.

If you look at the contents of the completed file and the text file is in XML format according to the reference article above, it should be fine.


2022-09-30 22:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.