Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Python modifies Word document styles in batches

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "Python how to modify Word document style in batches", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python how to modify Word document style in batches"!

The logic of the requirements is as follows:

1. Create an empty folder (for storing modified financial reports to avoid making direct changes to the original file)

two。 Get all the Word paths under the destination folder through glob

3. Use the docx module to traverse each Word document and get text blocks containing specific words

4. Make style changes to the text block

Code implementation

First import the required libraries and set the destination folder path

From docx import Documentfrom docx.shared import RGBColor, Pt, Cmimport osimport glob# change the path to create the folder here mkdir_path = r'C:Usersxxxew_dir'# change the location where all the files are located file_path = ringing Cpurs Usersxxxx'

We are familiar with os and glob. Here is a brief introduction to the docx module, which is a non-standard library. On the command line, we need to enter the following command to install:

Pip install python-docx

In particular, it is important to note that python-docx is entered during installation and docx is actually called

Instantiate the specific Word document code as follows:

From docx import Documentwordfile = Document (path)

If you do not specify a path, it is to create a new Word document, which is not quite the same as the openpyxl that operates Excel, so you will have the opportunity to introduce it again.

Now we set up a folder to store the newly generated files, first determine whether the folder exists, and then run os.mkdir if it does not exist to avoid reporting an error

If not os.path.exists (mkdir_path): os.mkdir (mkdir_path)

Then traverse the Word document, using the wildcards of the glob module, the framework is as follows:

For file in glob.glob (file_path +'/ * .docx'): pass

According to the three-level structure, a document Document has one or more paragraph Paragragh, and a paragraph Paragraph has one or more text blocks Run. The combined code is:

For file in glob.glob (file_path +'/ * .docx'): docx = Document (file) for paragraph in docx.paragraphs: for run in paragraph.runs: pass

Finally, we modify the style for specific words, and after traversing to the text block, we can make a judgment.

... For run in paragraph.runs: if 'funds' in run.text: run.font.bold = True # bold run.font.color.rgb = RGBColor (255,0,255) # set font color # finally remember to save docx.save (mkdir_path +'/'+ os.path.basename (file))

Of course, in addition to changing the font color and bold, other style settings are also supported. Here is a list of commonly used ones for reference:

# bold run.font.bold = True# italic run.font.italic = True# underline run.font.underline = True# delete line run.font.strike = True# font size run.font.size = Pt (20) # Shadow run.font.shadow = True# font color run.font.color.rgb = RGBColor (255,0,255)

At this point, the whole requirement ends smoothly, with the complete code:

From docx import Documentfrom docx.shared import RGBColor, Pt Cmimport osimport globmkdir_path = r'C:xxxew_dir)'if not os.path.exists (mkdir_path): os.mkdir (mkdir_path) keyword = 'funds' file_path = r'C:Usersxxx'for file in glob.glob (file_path +'* .docx'): docx = Document (file) for paragraph in docx.paragraphs: for run in paragraph.runs: if keyword in run.text: Run.font.bold = True run.font.color.rgb = RGBColor 0,0) docx.save (mkdir_path +'/'+ os.path.basename (file)) Thank you for your reading The above is the content of "Python how to modify Word document style in batches", after the study of this article, I believe you have a deeper understanding of how Python modifies Word document style in batches, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report