我需要帮助编写一个for循环来查找所有"。docx";文件然后对其进行编辑并以新名称保存。
- 我有一些适用于单个文件的代码
- 我需要在文件夹中迭代的代码;docx">
- 编辑后,将它们保存到另一个文件夹中。a( 保留*当前文件名并将其放在另一个文件夹中
下面的代码:
import docx
import os
directory = '.' #set dir of all *docx files"
extension = '.docx' # editable files must be "docx"
text = '' #not sure what this is doing here.
#The for loop to check that all the .docx are found in folder. Edit them regx. BUT I do not know the proper syntax for "file" ending in "docx"
for "file" in os.listdir(directory) #
if "file".endswith(extension)
def replacer(p, replace_dict):
inline = p.runs # Specify the list being used
for j in range(0, len(inline)):
# Iterate over the dictionary
for k, v in replace_dict.items():
if k in inline[j].text:
inline[j].text = inline[j].text.replace(k, v)
return p
# Replace Paragraphs
doc = docx.Document("5538AP_7975_D1.docx") # Get a file BUT I need all files with different part names.
dict = {'Don Steel': 'TAN', '5538AP':'5499AP', 'Special Mask Build Notes': 'Special Mask Build Notes: Special New receipts for TAN 4X to 5X'} # Build the dict
for p in doc.paragraphs: # If needed, iter over paragraphs
p = replacer(p, dict) # Call the new replacer function
doc.save("/newOrder/SUBSTITUTE_5538AP1.docx") # need to save all files with their original name in a new folder "newOrder".
一个简单但有效的答案,您可以使用glob
模块,因为它内置了直接模式搜索。参见以下示例
import glob
for i in glob.glob('E:drivers\**\*.pdf', recursive=True):
print(i)
这将查看E:Drivers
中的所有文件,然后查看每个子目录并查找pdf文件。您可以在文档页面上看到更多信息:https://docs.python.org/3/library/glob.html
# NEW FUNCTION:
import docx
import glob
for i in glob.glob(r'C:UsersLOCALDocumentsTOPPAN_MOR_BASE_PART*.docx', recursive=True):
def replacer(p, replace_dict):
inline = p.runs # Specify the list being used
for j in range(0, len(inline)):
# Iterate over the dictionary
for k, v in replace_dict.items():
if k in inline[j].text:
inline[j].text = inline[j].text.replace(k, v)
return p
# Replace Paragraphs
file_suffix = raw_input(i)
doc = docx.Document(".docx") # Get the file
dict = {'Don Coffman': 'TOPPAN', 'MG5459APSIM':'MG5499AP', 'Special Mask Build Notes': 'Special Mask Build Notes: Special Mask Build Notes: TOPPAN 4X to 5X'} # Build the dict
for p in doc.paragraphs: # If needed, iter over paragraphs
p = replacer(p, dict) # Call the new replacer function
doc.save(r'C:\Users\LOCAL\Documents\TOPPAN_MOR_BASE_PART\NewOrder\SUBSTITUTE'+file_suffix+'docx'(