首页 > 解决方案 > 重命名 pdf 重命名 pdf 文件及其文本内容在特定位置

问题描述

我找不到使用该enumerate功能重命名文件的方法。例子:

for position, line in enumerate(pdf_text):
    lines_to_read = [1280,1281,1282,1283,1284,1285,1286,1287,1288,1289,1290,1291]
    if position in lines_to_read
        print(line,end="") 
        # I can print the text sequence at specific conditions
        pdf_file_obj.close()
        # but i've not success for use the print output as file name 
        os.rename(fullpath,+ "??" + ".pdf")

如何转换输出中的字符串以将其用作文件名?

标签: pythonpython-3.xfile-rename

解决方案


我使用链式正则表达式解决了它。

我谢谢大家

for index in re.finditer("SHERAZ0101", pdf_text):
  #oc_ext = pdf_text[index.start()+506:index.start()+531]
  doc_ext = pdf_text[index.end()+488:index.end()+521] +pdf_text[index.end()+471:index.end()+477]
  
  doc_num = "" + doc_ext.replace('\n\n',"").replace('/',"-").replace('SPECI',"").replace(":","").replace("  ", "").replace(" .pdf",".pdf")
  pdf_file_obj.close()
  print(doc_ext)
  # rename the pdf based on the information in the pdf
  os.rename(fullpath, rename_folder + "\\" + doc_num + ".pdf")

推荐阅读