0

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])


Sup6
  • 21
  • 5
  • Please show some example input and output (see: [mcve]). As far as I can tell, this question has nothing to do with docx-python. – AMC Nov 30 '19 at 02:03
  • I already have the needed values, my question is: How do I put back in the docx paragraph the numbers that I have? I couldn't find a replace or search function in docx-python documentation. – Sup6 Nov 30 '19 at 10:07

1 Answers1

0

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']
Ayoub Benayache
  • 1,046
  • 12
  • 28
  • Thank you for your answer! But my question is how do I replace the text in docx file. How do I select a certain paragraph and modify it? – Sup6 Nov 30 '19 at 10:12
  • check this https://stackoverflow.com/a/24813382/8436879 – Ayoub Benayache Nov 30 '19 at 10:16
  • This answer doesn’t address the question by @Sup6, except in that link which proves it’s a duplicate...? – AMC Nov 30 '19 at 20:00