So I've been playing for a while with docx-python and couldn't figure out a way to replace the numbers from a string. I have a paragraph that is like this: NR 050 / 04.10.2019 I did manage to get the numbers separate, put them in a list and modify them as I will. How do I put back in the paragraph from the docx file the modified values? Here's my code so far:
document = Document('1.docx')
section = document.sections[0]
header = section.header
order = []
for paragraph in header.paragraphs:
print(paragraph.text)
for s in re.findall(r'\b\d+\b', paragraph.text):
int(s)
order.append(int(s))
print('Contract number:',order[0],'\nContract Date: ',order[1],order[2],order[3])
order[0] = order[0] + 1
print('New contract number: ',order[0])
today = date.today()
day = today.strftime("%d/%m/%Y")
data_curenta = []
for s in re.findall(r'\b\d+\b', day):
int(s)
data_curenta.append(int(s))
print('Current date: ',data_curenta[0],data_curenta[1],data_curenta[2])
order[1] = data_curenta[0]
order[2] = data_curenta[1]
order[3] = data_curenta[2]
print('New contract date: ',order[1],order[2],order[3])
the simple way to convert a string to number is to convert it to int, if the number is a float try this
num=" 44.545"
print(int(float(num)))
output
44
to separate string in list use split : your_text_var.split("eg. your seprator")
txt="123.45.67"
newvar= txt.split(".")
print(newvar)
output
['123', '45', '67']