看板 Python 關於我們 聯絡資訊
自行修改網路上pdf轉word的程式 執行後發現需要輸入兩次數字選項才會開始執行 想請問是什麼地方造成的? 感謝! os.chdir(os.path.abspath(os.path.dirname(sys.argv[0]))) J=True while J: print("""\n執行程式前請先將檔案放入資料夾中""") print("\n1.PDF_轉_Word\n2.PDF圖片擷取\n3.退出程式\n") choice=int(input("請輸數字選擇需要的功能:")) if choice==1: print("歡迎使用PDF轉Word程式") time.sleep(1.5) print("-----------Welcome to the program-----------") pdf_list=glob.glob("*.pdf") print(u"共發現%s個pdf文件"% len(pdf_list)) print(u"正在處理...") print(pdf_list) def convertPdf(fileName): with pdfplumber.open(fileName) as pdf: print("正在處理文件{0},共{1}頁...".format(fileName, len(pdf.pages))) content="" baseName=fileName.split(".")[0] wordName=baseName+".docx" flag=True if os.path.exists(wordName): os.remove(wordName) for i in range(len(pdf.pages)): print("正在處理<{0}>第{1}頁...".format(baseName, i)) page=pdf.pages[i] if page.extract_text()==None: print("{0}是圖片格式,無法轉換".format(fileName)) flag=False break page_content="\n".join(page.extract_text().split("\n")[:-1]) content=content+page_content if os.path.exists(wordName): doc=Document(wordName) else: doc=Document() doc.add_paragraph(content) doc.save(wordName) content="" print("<{0}>第{1}頁處理完成!".format(baseName, i)) if flag: print("{0}處理完成,保存為{1}。".format(fileName, wordName)) if __name__=="__main__": for file in os.listdir("."): if os.path.isfile(file) and file.split(".")[1]=="pdf": p=Process(target=convertPdf,args=(file,)) # 多工處理 p.start() break -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.137.185 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1641348013.A.A41.html
panex0845: 你的偵錯壞了嗎? 01/05 10:18
lycantrope: 每多個Process會import整個.py,導致while loop重複 01/05 10:51
lycantrope: 你要把while loop分開放在if__name__=="__main__": 01/05 10:51
lycantrope: 下面 01/05 10:51
chaojune: 還是不行@@ 感謝樓上回覆 01/06 09:04
lycantrope: 怎麼可能不行,你可以把code貼出來嗎? 01/06 09:18
lycantrope: https://reurl.cc/zMz35k 01/06 14:22
chaojune: 請收站內信 01/07 11:44
hanfadacai: 同L大 01/10 15:29