question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

only produce the path element, not the whole file

See original GitHub issue

I would like to embed the QR svg element produced by this library in my own svg producing software, and the only way I see to do that, is to write the file into a StringIO, then extracting the element I need.

according to the docstring for QRCode.svg,

If only the code itself is desired, set the xmldecl to false. This will result in a fragment that contains only the “drawn” portion of the code.

however, if I do that, I only get rid of the leading <?xml version="1.0" encoding="UTF-8"?>, not also the root svg element. I would like the naked path element, and I would like it returned in a string, not written to a file.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
heuercommented, Jul 26, 2017

If you use BytesIO, you’ll have to decode the value into the used encoding (default: UTF-8) before passing it into re.search / re.match

    def __call__(self, x, y, text, scale=1, encoding='utf-8'):
        qr = self.pyqrcode.create(text)
        self.buffer.truncate(0)
        self.buffer.seek(0)
        qr.svg(self.buffer, xmldecl=False, quiet_zone=0, scale=scale, encoding=encoding)
        match = self.pattern.search(self.buffer.getvalue().decode(encoding))
        result_list = [match.group(1)]
        if x != 0 or y != 0:
            result_list.insert(0, '<g transform="translate(%s,%s)">' % (x, y))
            result_list.append('</g>')
        return '\n'.join(result_list)
1reaction
heuercommented, Jul 26, 2017

Omitting the SVG element isn’t possible. It’s possible to omit additionally the namespace delaration; this is useful if you want to embed the generated QR Code in HTML files.

Anyway, extracting the path should be trivial.

BTW, you don’t have to write to a file:

>>> import io
>>> import pyqrcode
>>> qr = pyqrcode.create('Test')
>>> out = io.BytesIO()
>>> qr.svg(out, scale=10, xmldecl=False, svgns=False)
>>> out.getvalue()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get only the last part of a path in Python?
Use os.path.normpath , then os.path.basename : >>> os.path.basename(os.path.normpath('/folderA/folderB/folderC/folderD/')) 'folderD'.
Read more >
Paths - SVG: Scalable Vector Graphics - MDN Web Docs
The <path> element is the most powerful element in the SVG library of basic shapes. It can be used to create lines, curves,...
Read more >
Paths — SVG 2
A path is defined by including a 'path' element on which the d property specifies the path data. The path data contains the...
Read more >
Path Operations - Essential Java Classes - Oracle Help Center
A relative path cannot be constructed if only one of the paths includes a root element. If both paths include a root element,...
Read more >
Maximum Path Length Limitation - Win32 apps | Microsoft Learn
When using an API to create a directory, the specified path cannot be so long that you cannot append an 8.3 file name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found