作者chaojune ()
看板Python
标题[问题] python回圈问题
时间Wed Jan 5 10:00:11 2022
自行修改网路上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://webptt.com/cn.aspx?n=bbs/Python/M.1641348013.A.A41.html
1F:嘘 panex0845: 你的侦错坏了吗? 01/05 10:18
2F:推 lycantrope: 每多个Process会import整个.py,导致while loop重复 01/05 10:51
3F:→ lycantrope: 你要把while loop分开放在if__name__=="__main__": 01/05 10:51
4F:→ lycantrope: 下面 01/05 10:51
5F:→ chaojune: 还是不行@@ 感谢楼上回覆 01/06 09:04
6F:推 lycantrope: 怎麽可能不行,你可以把code贴出来吗? 01/06 09:18
8F:→ chaojune: 请收站内信 01/07 11:44
9F:推 hanfadacai: 同L大 01/10 15:29