0
want to create a PDF from a list but this resulting the following error:
C:\Users\nicperei\Desktop\pyCodes>py web.py
Traceback (most recent call last):
File "web.py", line 22, in <module>
pdf.drawString(100, 10, listaFinal)
File "C:\Users\nicperei\AppData\Roaming\Python\Python36\site-packages\reportlab\pdfgen\canvas.py", line 1560, in drawString
text = text.decode('utf-8')
AttributeError: 'list' object has no attribute 'decode'
C:\Users\nicperei\Desktop\pyCodes>
follows my code:
import requests
from bs4 import BeautifulSoup
from reportlab.pdfgen import canvas
url ='http://servicos2.sjc.sp.gov.br/servicos/horario-e-itinerario.aspx?acao=p&opcao=1&txt='
r = requests.get(url)
listaFinal = []
soup = BeautifulSoup(r.text, 'lxml')
lista_intinerarios = soup.find_all('table', class_='textosm')
urlsjc = 'http://www.sjc.sp.gov.br'
for lista_td in lista_intinerarios:
lista = lista_td.find_all('td')
for lista_dados in lista:
if lista_dados.next_element.name == 'a':
url_it = '{0}{1}'.format(urlsjc,lista_dados.next_element.get('href'))
listaFinal.append(url_it)
else:
listaFinal.append(lista_dados.next_element)
pdf = canvas.Canvas("Site_Prefeitura3.pdf")
pdf.drawString(100, 200,listaFinal)
pdf.save()
print(listaFinal)
print("Executado com sucesso...")
Thanks in advance!
Error says the function
pdf.drawText
takes only 2 arguments, but you passed 4.– Octávio Santana
I’ve changed from
pdf.drawText
forpdf.drawString
– Nicolas Pereira