[問題] 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
噓
01/05 10:18,
3年前
, 1F
01/05 10:18, 1F
推
01/05 10:51,
3年前
, 2F
01/05 10:51, 2F
→
01/05 10:51,
3年前
, 3F
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
01/06 09:18, 6F
推
01/06 14:22,
3年前
, 7F
01/06 14:22, 7F
→
01/07 11:44,
3年前
, 8F
01/07 11:44, 8F
推
01/10 15:29, , 9F
01/10 15:29, 9F
Python 近期熱門文章
PTT數位生活區 即時熱門文章