Is it possible to call a macro of notepad++ from python? - python-3.x

I have a macro that make changes to all files in a folder in notepad++ but I am trying to automatizate the process with a python code but I do not find any solution for this. Maybe I can use the shortcuts.xml in some way but I am totally lost.
This is my final solution if someone needs it, you MUST set your macro to a shortcut, in this case 'ctr+alt+9' (comments are on spanish):
#
# Es necesario instalar las bibliotecas
# pip install pyautogui
# pip install pywin32
#
# ----------------------------------------------------------------
# Version: 1.3
# Date: 10/07/2019
#
# Resumen: Pone Notepad++ como pantalla activa y pulsa ctrl+alt+9
# y acepta los pop ups de la macro
#
# Autor: Pedro Antonio Fondevila Franco
# ----------------------------------------------------------------
import pyautogui
import win32gui
import win32con
import time
# Copiado de Stackoverflow, retorna lista de las ventanas activas
# stackoverflow.com/questions/16770909/python-win32gui-setforegroundwindow
def window_enum_handler(hwnd, resultList):
if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) != '':
resultList.append((hwnd, win32gui.GetWindowText(hwnd)))
def get_app_list(handles=[]):
mlst=[]
win32gui.EnumWindows(window_enum_handler, handles)
for handle in handles:
mlst.append(handle)
return mlst
appwindows = get_app_list()
# ----------------------------------------------------------------
targetWindow = 0;
for window in appwindows:# Buscamos si notepad++ esta en la lista
if ("Notepad++" in window[1]):
targetWindow = window[0]
if(targetWindow != 0):
win32gui.ShowWindow(targetWindow,win32con.SW_RESTORE) # Por si esta minimizada la pantalla
win32gui.BringWindowToTop(targetWindow) # La ponemos delante
win32gui.SetForegroundWindow(targetWindow) # La ponemos como ventana principal
time.sleep(0.4) # Le damos tiempo a cambiar al notepad++
pyautogui.hotkey('ctrl', 'alt', '9') # Pulsa ctrl+alt+9
numberOfPopUps = 10
for i in range(1, numberOfPopUps + 1):
time.sleep(0.4)
pyautogui.press('left') # Pulsa izquierda
pyautogui.press('enter') # Pulsa enter
else:
print("Inicie Notepad++")
time.sleep(1.5)

You may want to use VBScript or PowerShell and Python in combination for your needs.
At Notepad++ Community is a thread How to run a notepad++ macro from vbscript or PowerShell.
Quoted from Yaron's answer:
objShell.run …
' Let NPP load and open the file. Adjust the number of milliseconds to your machine.
WScript.Sleep(100)
' "^+9" is Ctrl+Shift+9. Replace it with your preferred shortcut.
objShell.SendKeys("^+9")
Assign a shortcut to your macro via Settings -> Shortcut Mapper ->
Macros.
Add the above lines to the script.
You'll find a possible solution for the second part Executing a vbs file with arguments created by python on SO here.

Related

My python program does not write the accents in the Spanish language

this is my code
import pyautogui
import time
f = open('spambot\spam.txt', 'r')
time.sleep(5)
for words in f:
print(words)
pyautogui.typewrite(words)
pyautogui.press("enter")
time.sleep(3)
the content of spam.txt is this
"Yo sé que no me te importó"
but this is what it prints
"Yo s que no te import"
You can edit this line adding an encoding that is appropriate for Spanish: 'utf', 'utf-8' or 'utf-8-sig' work fine:
f = open('spam.txt', 'r', encoding='utf-8-sig')

Why can't i extract correctly the image from this pdf? [Please need help]

I am currently working on OCR on pdf files. Here is my pipeline:
i first extract image from pdf (since my pdf contained scanned document) and convert in numpy array
then i read with tesseract
It works pretty well on most of my image but i have sevral whose i can't extract the image inside. I just gave an example and i can't find (see next) the scanned image containing the writing part (for OCR). It drive me crazy (where has it gone ??).
Perhaps you could help me to retrieve that image and understand why my way do not let me retrieve this image "fantôme" ?
NB: i noticed that thoses problematic images inside the pdf are in "jpx" format.
Edit: Since the image is unfindable in the pdf i tried an horrible trick (waiting for clever explanation :) ): converting whole pdf page in pix (PyMuPdf let do that) and then writing the PIX on disk in different format (PNG, TIFF). The quality is too much degraded compared with the original pdf (so we can forget a reasonnable reading with Tesseract).
Here is the pdf example file (if you have simpler hosting way i am curious): https://www.filehosting.org/file/details/906817/IB00058815877D0000000.pdf
Here are the 2 images i extract from the file (the second one should contain txt instead of garbage)
Here is my code to extract images:
import fitz
import os
import logging
import cv2
from PIL import Image
from .utils import lazyproperty,showpdf
from .imhelpers import show
from ..config import myconfig
from impocr import logger
import pytesseract
pytesseract.pytesseract.tesseract_cmd = myconfig.TESSERACT_CMD
class InvalidImage(Exception):
pass
class PDFParser():
"""
"""
def __init__(self,filepath,page_num=0):
self.filepath = filepath
self.filename = os.path.basename(self.filepath).split('.pdf')[0]
try:
self._doc = fitz.open(filepath)
self.page_num = page_num
self._page = self._doc[page_num]
except Exception as e:
print("Lecture PDF impossible. {}".format(e))
raise
#lazyproperty
def text(self):
return self._page.getText()
#lazyproperty
def _pixs(self):
imgs = self._doc.getPageImageList(self.page_num)
pixs =[]
for img in imgs:
xref = img[0]
pix = fitz.Pixmap(self._doc, xref)
pixs.append(pix)
return pixs
#lazyproperty
def _pixpage(self):
pix = self._page.getPixmap(colorspace=fitz.csGRAY)
return pix
#property
def img(self):
return self.imgs[0]
#property
def pageimg(self):
pix = self._pixpage
return self.pix2np(pix)
#lazyproperty
def imgs(self):
pixs = self._pixs
imgsarray = []
for pix in pixs:
img = self.pix2np(pix)
imgsarray.append(img)
return imgsarray
def find_first_valid_image(self):
img_valid = None
for i,img in enumerate(self.imgs):
try:
import ipdb;ipdb.set_trace()
res = pytesseract.image_to_osd(img)
img_valid = img
return img_valid
except pytesseract.TesseractError:
continue
if img_valid==None:
logger.warning('No readable image in page {} of the document {}'.format(self.page_num, self.filename))
raise InvalidImage('No readable image in page {} of the document {}'.format(self.page_num, self.filename))
def write(self,outputdir,fullpage=False):
try:
os.makedirs(outputdir)
logger.info("Directory {} is created".format(outputdir))
except FileExistsError:
pass
def _writepix(pix,filepath):
# This is GRAY or RGB
try:
pix.writePNG(filepath)
# CMYK: convert to RGB first
except:
pix = fitz.Pixmap(fitz.csRGB, pix)
pix.writePNG(filepath)
pix = None
if fullpage:
filepath = os.path.join(outputdir,'{}_p{}.png'.format(self.filename,self.page_num))
pix = self._pixpage
_writepix(pix,filepath)
return
pixs = self._pixs
for i,pix in enumerate(pixs):
filepath = os.path.join(outputdir,'{}_p{}_i{}.png'.format(self.filename,self.page_num,i))
_writepix(pix,filepath)
return
def pix2np(self,pix):
"""
Convert pixmap to image np.ndarray
https://stackoverflow.com/questions/53059007/python-opencv
param pix: pixmap
"""
import numpy as np
#https://stackoverflow.com/questions/22236749/numpy-what-is-the-difference-between-frombuffer-and-fromstring
im = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.h, pix.w, pix.n)
try:
im = np.ascontiguousarray(im[..., [2, 1, 0]]) # rgb to bgr
except IndexError:
#Trick to convert Gray rto BGR, (im.reshape)
#logger.warning("Need to convert Gray to BGR [filepath: {}]".format(self.filepath))
im = cv2.cvtColor(im,cv2.COLOR_GRAY2RGB)
im = np.ascontiguousarray(im[..., [2, 1, 0]])
return im
if __name__ == "__main__":
filepath = r'data\inputs\test\impot_textpdf_with_one_logoimage.pdf'
###### Parse page 0 (first page) ######
pdf = PDFParser(filepath,0)
text = pdf.text
imgs = pdf.imgs
show(pdf.imgs[0])
show(pdf.imgs[1])
############### other functions ####################
class lazyproperty:
def __init__(self, func):
self.func = func
def __get__(self, instance, cls):
if instance is None:
return self
else:
value = self.func(instance)
setattr(instance, self.func.__name__, value)
return value
def show(image):
import matplotlib.pyplot as plt
fig,ax = plt.subplots(1)
ax.imshow(image)
plt.show()
My solution is not so good (but waiting for better ideas but here are my 2 cents idea: i first write the full page and read it with opencv (i changed the method first_valid_image as you can see attached).
from tmpfile import TemporaryDirectory
def find_first_valid_image(self):
#import ipdb;ipdb.set_trace()
img_valid = None
for i,img in enumerate(self.imgs):
try:
#import ipdb;ipdb.set_trace()
res = pytesseract.image_to_osd(img)
img_valid = img
return img_valid
except pytesseract.TesseractError:
continue
if img_valid==None:
logger.warning('No readable image in page {} of the document {}. Tried the fullpage.'.format(self.page_num, self.filename))
with TemporaryDirectory() as tmpdirname:
filepath = self.write(tmpdirname,fullpage=True)
img_fullpage =cv2.imread(filepath)
return img_fullpage
I think it degrade the quality of my original image; so when applying tesseract on the image i got a bad ocr as you can see attached.
"""DIRECTION GÉNÉRALE DE6 FNANCES PUBLIQUES\n\nAVIS D'IMPÔT 2017\nIMPÔT SUR LES REVENUS\nd Fannée 2016\n\n \n\nPour vos _ démarches,\npas besoin doiginal —\nMc d furir un —\nphotocopie, vérifiable sur —\nTmpots gouv vn\n\nVotre situation\n\n \n\nVos rétérences.\n\nPour accéder à votre espace partculior MONTANT À PAYER\nNuméro fiscal | | A us ario 15/00/2017 (41)\n\nN* daccès en ligne voirvouo déciaration | | Détail du montant à payer\nRevenu fiscal d référence Montart de vtr impôt su e revors\n# | Rétéronce de 'avis <VRRRRS | Versemens sur 1er acompte\nVersomontssur 26 acompto\n\nNuméro F —\n\nNuméro de rôle 016 A\nDate c'étaissement 2m0762017|\nDate de mise en recouvrement 3vo7æ2017|\n\n \n\n \n\n \n\n3899,00 €\n3893006\n\n \n\n \n\nLa somme que vous davez payer est supérieure à 2 000 €\nLa loirend obligatoie le paiement de cette somme par un des moyens suivants, à votre choix :\n\nur impots.gouv.fr: payez en igne ou adhérez au prélèvement à léchéance en vous connectant à vore\nspaco pariclor, pislissoz-vous guider\n\npartéléphone, courrir où couriel pour adhérer au prélèvement à échéanco (aux coordonnéesindiquées\ndansle cadre - Vos démarches »\n\nPour 2018,vous pourrez achérerau prélèvement mensue\n\x0c"""

How can i create sound alert when a cell in jupyter notebook finishes running?

Is it possible to customize jupyter to play sound or some alert when it finishes running a cell?
You have several options. This is a little hack I use for most of my applications. It runs on Mac and Windows:
def Beep(freq = 2100, duration = 300, repeat=3, sound_file=None):
"""Beep by FC:
Funciona apenas Win e Mac
freq = 2100 (frequência)
duration = 300 (duração em milissegundos)
repeat = 3 (número de repetições)
sound_file = Submarine, Ping, Purr, Hero, Funk, Pop, Basso, Sosumi, Glass, Blow, Bottle, Frog, Tink, Morse
(*) No Mac o único parâmetro válido é o número de repetições.
A base para o "Beep" é o system/afplay
"""
plataforma = str(platform.uname()).lower()
if not len ([s for s in ['windows','win32','win64'] if s in plataforma]) == 0:
import winsound
for i in range(repeat):
winsound.Beep(freq,duration)
elif not len ([s for s in ['darwin'] if s in plataforma]) == 0:
import os
for i in range(repeat):
if sound_file is not None:
if os.path.isfile(f"""/System/Library/Sounds/{sound_file}.aiff"""):
os.system(f"""afplay /System/Library/Sounds/{sound_file}.aiff""")
else:
print(f"""/System/Library/Sounds/{sound_file}.aiff *NOT FOUND*""")
break
else:
os.system('afplay /System/Library/Sounds/Tink.aiff')
else:
print('Sorry, plataforma não identificada. Vc está usando Linux?')
return
#Beep(sound_file='Hero', repeat=1)
Basically, if you are on Windows you have natively winsound. On Mac you have to play a sound manually, such as below:
from playsound import playsound
playsound('/path/to/note.wav') # .wav file
playsound('/path/to/note.mp3') # .mp3 file
However, I strongly suggest you use use jupyternotify, as you can see here.

Export seen items in QTreeView

I have a new question, I am designing an application and I don not know how to export items to an excel file, in the code I use a QTreeView with a QSortFilteredProxyModel, and I include a filter for text written in a QLineEdit, I need to export all of the items than can be seen after being filtered. But, how can i take or add those items to a list, for exporting them to an excel file?
if anyone can help me I would be so grateful.
sry my bad english
# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from dbFacades.profitFacade import ProfitFacade
class MainTabProfits(QGridLayout):
def __init__(self):
super(MainTabProfits, self).__init__()
self.screen = QDesktopWidget().screenGeometry()
self.profitList = ProfitFacade().allProfit()
self.addWidget(self.topGroupBox(),0,0,1,4)
self.addWidget(self.rightGroupBox(), 1, 0,1,4)
self.addWidget(self.botGroupBox(),2,3)
# -----------------------------------------------------
# Se define el grupo superior
def topGroupBox(self):
groupBox = QGroupBox()
groupBox.setTitle("Busqueda")
self.lineEdit1 = QLineEdit()
self.lineEdit1.setPlaceholderText("Ingrese una fecha con el siguiente formato -> dd/mm/aaaa")
self.lineEdit1.textChanged.connect(self.filterRegExpChanged)
self.lineEdit1.textChanged.connect(self.setSlashChanged)
self.lineEdit1.setMaxLength(10)
leftLayout = QHBoxLayout()
leftLayout.addWidget(self.lineEdit1)
leftLayout.stretch(1)
leftGroupBox = QGroupBox("Fecha Exacta")
leftGroupBox.setLayout(leftLayout)
hBoxLayout = QHBoxLayout()
hBoxLayout.addWidget(leftGroupBox)
groupBox.setLayout(hBoxLayout)
return groupBox
# -----------------------------------------------------
# Se define el grupo principal(izquierdo) de contenido
def rightGroupBox(self):
groupBox = QGroupBox()
groupBox.setTitle("Control de Finanzas y busqueda")
self.setHeaders()
self.proxyModel = QSortFilterProxyModel()
self.proxyModel.setDynamicSortFilter(True)
self.proxyModel.setSourceModel(self.model)
self.proxyModel.setFilterKeyColumn(0)
self.treeView = QTreeView()
self.treeView.setRootIsDecorated(False)
self.treeView.setAlternatingRowColors(True)
self.treeView.setModel(self.proxyModel)
self.treeView.setSortingEnabled(True)
self.treeView.sortByColumn(0, Qt.AscendingOrder)
self.treeView.resizeColumnToContents(0)
self.treeView.setEditTriggers(QTreeView.NoEditTriggers)
vBoxLayout = QVBoxLayout()
vBoxLayout.addWidget(self.treeView)
groupBox.setLayout(vBoxLayout)
self.setProfit()
return groupBox
def botGroupBox(self):
groupBox = QGroupBox()
groupBox.setTitle("Exportacion")
self.radioButton0 = QRadioButton("*.PDF")
self.radioButton0.setIcon(QIcon("icons/pdf.png"))
self.radioButton0.setIconSize(QSize(self.screen.width() / 50, self.screen.width() / 50))
self.radioButton1 = QRadioButton("*.Excel")
self.radioButton1.setIcon(QIcon("icons/excel.png"))
self.radioButton1.setIconSize(QSize(self.screen.width() / 50, self.screen.width() / 50))
pushButton = QPushButton(QIcon("icons/export.png"),"Exportar")
pushButton.released.connect(self.exportList)
pushButton.setIconSize(QSize(self.screen.width() / 50, self.screen.width() / 50))
hBoxLayout = QHBoxLayout()
hBoxLayout.addWidget(self.radioButton0)
hBoxLayout.addWidget(self.radioButton1)
hBoxLayout.addWidget(pushButton)
hBoxLayout.stretch(1)
groupBox.setLayout(hBoxLayout)
return groupBox
# ------------------------------------------------------
# ZONA METODOS
# Metodo que define las cabezeras del menu y crea el model
def setHeaders(self):
self.model = QStandardItemModel(0, 5, self)
self.model.setHeaderData(0, Qt.Horizontal, "Fecha")
self.model.setHeaderData(1, Qt.Horizontal, "Monto")
self.model.setHeaderData(2, Qt.Horizontal, "Mesa")
self.model.setHeaderData(3, Qt.Horizontal, "Tipo de Pago")
self.model.setHeaderData(4, Qt.Horizontal, "Codigo Operacion")
# Metodo para filtrar por texto escrito en line edit
def filterRegExpChanged(self):
regExp = QRegExp(self.lineEdit1.text(), Qt.CaseInsensitive)
self.proxyModel.setFilterRegExp(regExp)
# Metodo para el formato de fecha en LineEdit1
def setSlashChanged(self):
text = self.lineEdit1.text()
if(len(text)==2):
self.lineEdit1.setText(text+"-")
elif(len(text)==5):
self.lineEdit1.setText(text+"-")
self.lineEdit2.setText("")
self.lineEdit3.setText("")
# Metodo para el formato de fecha en lineEdit2 y 2
def inRangeChanged(self):
text = self.lineEdit2.text()
text1 = self.lineEdit3.text()
if(len(text)==2):
self.lineEdit2.setText(text+"-")
elif(len(text)==5):
self.lineEdit2.setText(text+"-")
elif(len(text1)==2):
self.lineEdit3.setText(text1+"-")
elif(len(text1)==5):
self.lineEdit3.setText(text1+"-")
self.lineEdit1.setText("")
# --------------------------------------------------------------------------------
# Metodo que establece los datos en la lista
def setProfit(self):
for profit in self.profitList:
self.model.insertRow(0)
self.model.setData(self.model.index(0,0), profit[0])
self.model.setData(self.model.index(0,1), profit[1])
self.model.setData(self.model.index(0,2), profit[2])
self.model.setData(self.model.index(0,3), profit[3])
self.model.setData(self.model.index(0,4), profit[4])
# --------------------------------------------------------------------------------
# Metodo para exportar a excel
def exportList(self):
if self.radioButton0.isChecked():
pass
if self.radioButton1.isChecked():
HERE I WANT EXPORT ITEMS FILTERED
HERE I WANT EXPORT ITEMS FILTERED
HERE I WANT EXPORT ITEMS FILTERED
HERE I WANT EXPORT ITEMS FILTERED
You can use the fallowing functions from QSortFilterProxyModel:
rowCount(), columnCount() and index(), and then use the returned index to get the data:
rowCnt = self.proxyModel.rowCount()
colCnt = self.proxyModel.columnCount()
for row in range(0, rowCnt):
for col in range(0, colCnt):
modelIndex = self.proxyModel.index(row, col)
print modelIndex.data().toString()

Python 3: QString on PyQt4

I'm having a problem trying to do a very simple user interface. I made my UI with Qt Designer, and then with pyuic4 I got my python code. Then I programmed the function I needed, and compiled with Eclipse IDE.
The code I got from pyuic4 is:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Dni.ui'
#
# Created: Sat Apr 14 02:44:34 2012
# by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(371, 217)
Dialog.setMinimumSize(QtCore.QSize(371, 217))
self.layoutWidget = QtGui.QWidget(Dialog)
self.layoutWidget.setGeometry(QtCore.QRect(30, 30, 311, 151))
self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.layoutWidget)
self.gridLayout.setMargin(0)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.label = QtGui.QLabel(self.layoutWidget)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout.addWidget(self.label)
self.entrada = QtGui.QLineEdit(self.layoutWidget)
self.entrada.setObjectName(_fromUtf8("entrada"))
self.horizontalLayout.addWidget(self.entrada)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.boton = QtGui.QPushButton(self.layoutWidget)
self.boton.setObjectName(_fromUtf8("boton"))
self.gridLayout.addWidget(self.boton, 1, 0, 1, 1)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.label_3 = QtGui.QLabel(self.layoutWidget)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.horizontalLayout_2.addWidget(self.label_3)
self.salida = QtGui.QLineEdit(self.layoutWidget)
self.salida.setObjectName(_fromUtf8("salida"))
self.horizontalLayout_2.addWidget(self.salida)
self.gridLayout.addLayout(self.horizontalLayout_2, 2, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Dialog", "Introduzca su DNI", None, QtGui.QApplication.UnicodeUTF8))
self.boton.setText(QtGui.QApplication.translate("Dialog", "Hallar NIF", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("Dialog", "NIF:", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
And the code I made with the function I need:
from Dni import Ui_Dialog
from PyQt4 import QtCore, QtGui
LETRADNI = {0:'T', 1:'R', 2:'W', 3:'A', 4:'G', 5:'M', 6:'Y', 7:'F', 8:'P', 9:'D', 10:'X', 11:'B', 12:'N',
13: 'J', 14:'Z', 15:'S', 16:'Q', 17:'V', 18:'H', 19:'L', 20:'C', 21:'K', 22:'E'}
# Se hereda de la clase QtGui.QMainWindow
class Principal(QtGui.QMainWindow):
# Se define el constructor de la clase __init__
def __init__(self):
# Se llama al constructor de la clase padre
QtGui.QMainWindow.__init__(self)
# Se crea la instancia de Ui_Dialog
self.ventana = Ui_Dialog()
self.ventana.setupUi(self)
# Se conectan las señales con los slots
self.connect(self.ventana.boton,QtCore.SIGNAL('clicked()'), self.letradni)
def Calcula_letra_dni(dni):
'''Función Calcula_letra_dni:
Funcionamiento:
La función recibe el valor entero dni. Posteriormente calculará el resto de la división
por 23. Éste número se buscará en el diccionario 'LETRADNI' para obtener la letra correspondiente
a ese DNI.
Argumentos
dni -- número del documento nacional de identidad (int)
Devuelve:
Una cadena (string) -- DNI + letra preparado para salida por pantalla
'''
#if len(str(dni))>8 & len(str(dni))<7:
# raise ValueError('El dni debe tener entre 7 y 8 cifras')
num_letra = dni % 23.0
letra = LETRADNI[num_letra]
return '{0}-{1}'.format(dni,letra)
def letradni(self):
self.ventana.salida.setText(Calcula_letra_dni(self.ventana.entrada.text()))
The first one compiles and runs, it shows my ui perfectly.
Compiling the second one I get an error that says:
Description Resource Path Location Type
Undefined variable from import: QString Dni.py /Dni line 18 PyDev Problem
Can anyone help me?
Thanks in advance.
First off, I think your actual listed problem is related to Eclipse, pydev, and your projects PYTHONPATH. Review this to make sure you have properly set up everything and included PyQt4 in your pythonpath:
http://popdevelop.com/2010/04/setting-up-ide-and-creating-a-cross-platform-qt-python-gui-application/
After that, you seem to have some problems with your code beyond what you have mentioned...
First you define Principal class, then a Calcula_letra_dni function, but then you are defining what looks like a class instance method letradni which should be part of Principal:
class Principal(QtGui.QMainWindow):
# Se define el constructor de la clase __init__
def __init__(self):
...
def letradni(self):
...
def Calcula_letra_dni(dni):
...
Then it looks like you will raise an exception when you try to do math on a string (thanks #Avaris) and float:
num_letra = dni % 23.0
You should probably convert that string to a float first: num_letra = float(dni) % 23.0
And finally, I think you also forgot to define a main for your application. You have the one that is autogenerated in your Dni.py, but you didn't write one for your actual entry point script:
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
form = Principal()
form.show()
sys.exit(app.exec_())

Resources