[問題] python迴圈問題

看板Python作者時間3年前 (2022/01/05 10:00), 編輯推噓3(414)
留言9則, 4人參與, 最新討論串1/1
自行修改網路上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

01/05 10:18, 3年前 , 1F
你的偵錯壞了嗎?
01/05 10:18, 1F

01/05 10:51, 3年前 , 2F
每多個Process會import整個.py,導致while loop重複
01/05 10:51, 2F

01/05 10:51, 3年前 , 3F
你要把while loop分開放在if__name__=="__main__":
01/05 10:51, 3F

01/05 10:51, 3年前 , 4F
下面
01/05 10:51, 4F

01/06 09:04, 3年前 , 5F
還是不行@@ 感謝樓上回覆
01/06 09:04, 5F

01/06 09:18, 3年前 , 6F
怎麼可能不行,你可以把code貼出來嗎?
01/06 09:18, 6F

01/06 14:22, 3年前 , 7F

01/07 11:44, 3年前 , 8F
請收站內信
01/07 11:44, 8F

01/10 15:29, , 9F
同L大
01/10 15:29, 9F
文章代碼(AID): #1XrFkjf1 (Python)
文章代碼(AID): #1XrFkjf1 (Python)