Python3 html to pdf - html

how to convert HTML to PDF in Python3?
Xhtml2pdf does not work in Python3, got error:
import xhtml2pdf.pisa as pisa
Traceback (most recent call last):
File "", line 1, in
File "/home/hound/test/python/test_env/lib/python3.4/site-packages/xhtml2pdf/init.py", line 41, in
from xhtml2pdf.util import REPORTLAB22
File "/home/hound/test/python/test_env/lib/python3.4/site-packages/xhtml2pdf/util.py", line 302
raise Exception, "box not defined right way"
^
SyntaxError: invalid syntax

The best that I found by far is weasyprint.
From the documentation:
from weasyprint import HTML
HTML('http://weasyprint.org/').write_pdf('/tmp/weasyprint-website.pdf')
and it really works that easy. Saved me tons of time (after I wasted time trying to get xhtml2pdf and others to work in python 3 but failed.

I had the same error. Apparently for now xhtml2pdf has Python3 support only in its prerelease version - 0.2b1 (for more info see https://pypi.python.org/pypi/xhtml2pdf). I’ve solved the problem by uninstalling the previous xhtml2pdf version and installing the prerelease version
pip install --pre xhtml2pdf

Related

I am encountering module loading problem in mysql

I will work with mysql, but it gives this error anyway. I did delete the module but it did not change, I loaded it differently, I could not solve the modules.
Traceback (most recent call last):
File "C:\Users\INAL\Desktop\Son Scriptler\mysql\mysql.py", line 1, in <module>
import mysql.connector
File "C:\Users\INAL\Desktop\Son Scriptler\mysql\mysql.py", line 1, in <module>
import mysql.connector
ModuleNotFoundError: No module named 'mysql.connector'; 'mysql' is not a package
You might not have the mysql-connector did you try installing it using pip?
pip3 install mysql-connector
The problem was that my project name was mysql.py and I solved the problem by changing the project name.

Windows 10 Rtree installation successful from .whl file, but error when running code

I am running Python 3.7, 64bit on Windows 10 and trying desperately to get Rtree running. I use the package Rtree-0.9.1-cp37-cp37m-win_amd64.whl from Christoph Gohlke (https://www.lfd.uci.edu/~gohlke/pythonlibs/).
I have tried for very long to get it to work, but keep on getting the following error message when running a script that uses geopandas.
Traceback (most recent call last):
File "C:\Python37\lib\site-packages\rtree\core.py", line 90, in <module>
rt = ctypes.CDLL(os.path.join(here, 'spatialindex_c.dll'))
File "C:\Python37\lib\ctypes\__init__.py", line 364, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
The installation of the whl-package should include the libspatialindex files, but they are not found when running the code. I tried to use Python 2.7 first to run it, then installed Python 3.7. I've checked all the dependencies and checked whether the "spatialindex_c.dll" files are at the right place, but nothing helps. Would be great to get an answer on that.

Python 3: parse html with XPath error

I am new to python 3. I am paring the html data with XPath. I use the pycharm to compile my code, my code is shown as following. please help me fix the issue (please don't use the beautiful soup).I know a lot code about parse html with python 2 xpath, if you have some materials link about parse html with python 3 xpath, please tell me. I have installed the lxml and requests library in the pycharm. Further, the terminal default is python 2.7.Thanks in advance!
from lxml import html
import requests
page = requests.get('http://econpy.pythonanywhere.com/ex/001.html')
tree = html.fromstring(page.content)
#This will create a list of buyers:
buyers = tree.xpath('//div[#title="buyer-name"]/text()')
#This will create a list of prices
prices = tree.xpath('//span[#class="item-price"]/text()')
print('Buyers: ', buyers)
print('Prices: ', prices)
The errors:
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5
/Users/tianke0711/PycharmProjects/database/Pax_html/xpath_test.py
Traceback (most recent call last):
File
"/Users/tianke0711/PycharmProjects/database/Pax_html/xpath_test.py",
line 1, in <module>
from lxml import html
File
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-
packages/lxml/html/__init__.py", line 54, in <module>
from .. import etree
ImportError:
ddlopen(/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/lxml/etree.cpython-35m-darwin.so, 2): Library not loaded: libxml2.2.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/lxml/etree.cpython-35m-darwin.so
Reason: Incompatible library version: etree.cpython-35m-darwin.so requires version 12.0.0 or later, but libxml2.2.dylib provides version 10.0.0
Based on the error information,the libxml2.2( Library not loaded: libxml2.2.dylib) is the old version. Since the python3 need new version of libxml. Use the following command to install the new libxml, and then it works for me.
brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force
Actually, I don't know the reason in detail. If some guys know this, please tell me! Thanks!

Kivy pygame error

I've been trying to get Kivy to work on my Mac (Lion), but I've been encountering issues. I followed the instructions on the Kivy site, and since Kivy 1.8 supports Python 3, I wanted to run it with 3.3, and I finally got that to work, by editing the kivy file to point to 3.3 instead of 2.7. I tried drop a .py program on the Kivy icon, the app opened but nothing happened. So I tried to run it from the command line. It opened 3.3, as expected, but I got the following error.
Python 3.3.4 (default, Mar 6 2014, 20:14:14)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Applications/Kivy.app/Contents/Resources/lib/sitepackages/pygame/__init__.py", line 127, in <module>
from pygame.base import *
ImportError: dlopen(/Applications/Kivy.app/Contents/Resources/lib/sitepackages/pygame/base.so, 2): Symbol not found: _PyCObject_Type
Referenced from: /Applications/Kivy.app/Contents/Resources/lib/sitepackages/pygame/base.so
Expected in: flat namespace
in /Applications/Kivy.app/Contents/Resources/lib/sitepackages/pygame/base.so
I have no idea why I would get this error, since I previously installed pygame for 3.3, and import pygame or from pygame.base import * work error free. Would this issue with pygame explain why .py files fail to execute when I drop them onto the Kivy icon?
Kivy.app and all the dependencies included in it is compiled with and for Python 2.7. You cannot use it for 3.3.
If you want to try Kivy with 3.3, you also need to compile Kivy yourself :)

Error when trying to install Django CMS in virtualenv w/ MySQL on Mac OSX

I am getting the following error when I try to install django CMS with MySQL using virutalenv:
RuntimeError: maximum recursion depth exceeded
/Users/ethan/Sites/env/build/mysql-python/distribute-0.6.28-py2.7.egg
Traceback (most recent call last):
File "<string>", line 17, in <module>
File "/Users/ethan/Sites/env/build/mysql-python/setup.py", line 7, in <module>
use_setuptools()
File "distribute_setup.py", line 145, in use_setuptools
return _do_download(version, download_base, to_dir, download_delay)
File "distribute_setup.py", line 125, in _do_download
_build_egg(egg, tarball, to_dir)
File "distribute_setup.py", line 116, in _build_egg
raise IOError('Could not build the egg.')
IOError: Could not build the egg.
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /Users/ethan/Sites/env/build/mysql-python
The command I am using for the install is the following:
env/bin/pip install --download-cache=~/.pip-cache -r reqs.txt
And the reqs.txt file looks like this:
django-cms==2.4.1
Django==1.5.1
django-classy-tags==0.4
South==0.8.1
html5lib==1.0b1
django-mptt==0.5.2
django-sekizai==0.7
six==1.3.0
Pillow==2.0.0
django-filer==0.9.4
cmsplugin-filer==0.9.5
django-reversion==1.7
mysql-python==1.2.4
I'm a novice to django (just going through the tutorial) and I cannot find anything in a search that seems to resolve this issue. Anyone have any ideas?
Change your requirement to mysql-python==1.2.5, I had the same problem on a CentOS 6.3 server.
mysql-python v. 1.2.4 has a hard dependency on distribute version 0.6.28. It might not be picking the right version of distribute.
Run this after the virtual env is activated:
easy_install distribute==0.6.28
and you should be good to proceed with
env/bin/pip install --download-cache=~/.pip-cache -r reqs.txt
change requirement to mysql-python==1.2.5 also worked for me on OS X mavericks with pip install