I am trying to make some edits to a docx file… making a number into letter (i.e. if the variable is equal to 01 = ‘one’, equal to 02 = ‘two’, and so on, but in Spanish). The problem is that the variable f_dia_nom doesn’t work … it doesn’t even print anything… am I doing something wrong?? or am I missing something??
#!/usr/bin/env python2.6
from Tkinter import *
from docx import *
import tkMessageBox
root = Tk()
nombre = ""
exp_no = ""
ubic = ""
munic = ""
prov = ""
f_dia = ""
f_dia2 = ""
f_dia_nom = ""
def nombre_dia():
if f_dia2 == 1 or f_dia2 == 01:
f_dia_nom = "Un"
elif f_dia2 == 2 or f_dia2 == 02:
f_dia_nom = "Dos"
elif f_dia2 == 3 or f_dia2 == 03:
f_dia_nom = "Tres"
elif f_dia2 == 4 or f_dia2 == 04:
f_dia_nom = "Cuatro"
elif f_dia2 == 5 or f_dia2 == 05:
f_dia_nom = "Cinco"
elif f_dia2 == 6 or f_dia2 == 06:
f_dia_nom = "Seis"
elif f_dia2 == 7 or f_dia2 == 07:
f_dia_nom = "Siete"
else:
f_dia_nom = "Error"
# Hacer el docx
def makedocx():
if __name__ == '__main__':
# Default set of relationshipships - these are the minimum components of a document
relationships = relationshiplist()
# estructura del documento
document = opendocx('test.docx')
docbody = document.xpath('/w:document/w:body',namespaces=nsprefixes)[0]
# Buscar y reemplazar
print 'Replacing ...',
docbody = replace(docbody,'V_EXP',en1.get())
docbody = replace(docbody,'V_NOMBRE',en0.get())
docbody = replace(docbody,'V_OPERACION',op.get())
docbody = replace(docbody,'V_UBIC',en3.get())
docbody = replace(docbody,'V_MUNI',en4.get())
docbody = replace(docbody,'V_PROV',en5.get())
docbody = replace(docbody,'V_F_DIA',en6.get())
docbody = replace(docbody,'V_F_MES',mes.get())
docbody = replace(docbody,'V_F_SEM',sem.get())
docbody = replace(docbody,'V_NUM_DIA',en7.get())
nombre_dia()
docbody = replace(docbody,'V_NOM_DIA',f_dia_nom)
print 'f_dia_nom'
print 'done.'
Look up ‘global scoping’ rules for python. In general, try to avoid global variables as much as possible(if only to avoid errors like these)
in nombre_dia():
or any other assignment for that matter has the python compiler create that number LOCAL to the nombre_dia function.
modify your function to declare f_dia_nom as a global:
It will have nombre_dia SEE the f_dia_nom as the global
References
Learning Python 4th edition pg 408