我正在尝试从Word文件中读取表单并减去特定字符串作为路径并创建一个目录。但我在搜索目标字符串时收到错误消息。
import docx
path = 'C:/new/form.docx'
doc = docx.Document(path)
table = doc.tables[1]
for row in table.rows:
for cell in row.cells:
# Extract and process cell text
cell_text = cell.text.strip()
print(cell_text)
def between(value, a, b):
# Find and validate before-part.
pos_a = value.find(a)
if pos_a == -1: return ""
# Find and validate after part.
pos_b = value.rfind(b)
if pos_b == -1: return ""
# Return middle part.
adjusted_pos_a = pos_a + len(a)
if adjusted_pos_a >= pos_b: return ""
return value[adjusted_pos_a:pos_b]
test = "C:\new"
list = between(cell_text, "Data Source (Only)", "Working Folders")
print(between(cell_text, "Data Source (Only)", "Working Folders"))
import os
root_path = 'C:/new'
for items in list:
path = os.path.join(root_path, items)
os.mkdir(path)
cell_text中的内容如下:
.....
.....
Data Source (Only)
T:\vendor
Working Folders
(New created)
我想要做的是选择路径“T:endor”并分配给“list while import os”。
错误信息:
File "C:\test.py", line 14
def between(value, a, b):
^
IndentationError: unexpected indent
此错误与您的代码无关。您必须删除 def 之前的空格:
def between(value, a, b):