How to convert DOCX to DOCX in same format (without effecting font ,styles and font size) using python.

import docx
from docx import Document
import pandas as pd
list_elements=
doc = docx.Document("/content/drive/MyDrive/Colab Notebooks/Localization/Case Study 3 - 20.1.2022_Hindi.docx")
paras = doc.paragraphs
for para in paras:
text = para.text
size = ‘None’
if para.style.font.size is not None:
size = para.style.font.size.pt
style = para.style.name
alignment = para.alignment
color = para.style.font.color.rgb
font_type = None
for run in para.runs:
if run.bold:
font_type = ‘bold’
elif run.italic:
font_type = ‘italic’
else:
font_type = None
list_elements.append([text,style,size,alignment,font_type,color])
df = pd.DataFrame(list_elements,columns=[‘text’,‘style’,‘size’,‘alignment’,‘Font_type’,‘color’])

What do you mean, “convert DOCX to DOCX”?

If the file is already DOCX, you don’t need to do anything to make it DOCX. It is already DOCX.

Maybe you mean you just want to make a copy of the file with no changes?

import shutils
shutils.copyfile(oldfilename, newfilename)

will make a new DOCX from the old DOCX file without changing anything.

If this is not what you mean, please explain in words, not code, what you are trying to do.

Thanks for your response Steven.

Here i am sharing my use cases.

1 First i want to read Docx(1) file(Paragraphs and Tables)
2 Second i want to made some changes in Docx file(like removing unnecesary text,adding some paragraghs).
3 finally i want to write Docx(2) file same as source format Docx(1).

Queries:
1 How can i get all the font properties from Docx file using python.
2 I want using python how we can create Docx file by programmatically.

1 Like

This looks like (hidden) theft of a file … ?!

Did you try googling it? For problems that take the form “How can I do [high-level task]”, searching for a few keywords plus “python” is usually a much faster, easier and more efficient way to get a detailed solution almost instantly, and less work for both you and us. It is highly likely for any beginner question you are likely to ask that others have asked it before, and there exist a substantial body of material explaining how to do it.

For example, I simply searched for edit docx python and immediately was presented with a variety of easily accessible and directly relevant tutorials, answers and documentation using the python-docx package to do what you want.

To paraphrase the old aphorism, “Give a person a search and they have a search for a day; teach a person to search and they can search for a lifetime.”