我的博客:https://justlovesmile.top
最近不知道写什么了,正好昨天整理了几学期的年级排名,需要pdf转excel,所以百度学习了一下python的pdfplumber库
但是pdfplumber只能解析规整的完美的表格,那种乱七八糟的格式的表格,就不太行了,好在成绩单除了标题外,还算规整.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import pdfplumber # pip install pdfplumber from openpyxl import Workbook #pip install openpyxl import os wb = Workbook() # 创建文件对象 ws = wb.active # 获取第一个sheet path=os.getcwd()+"/2.pdf" #当前路径下的pdf文件 pdf = pdfplumber.open(path) #打开pdf文件 print('\n') print('开始读取数据') print('\n') #第一页第一行标题,解析只对规整的表格有用,凸(艹皿艹 ) #ws.append(pdf.pages[0].extract_tables()[0][0]) for page in pdf.pages: # 获取当前页面的全部文本信息,包括表格中的文字 #print(page.extract_text()) for table in page.extract_tables(): # print(table) for row in table: # print(row) #把列表拆了 rowlist=str(row).replace("[","",).replace("]","").replace("'","").replace("\\n","").split(",") #print(rowlist) ws.append(rowlist) print('---------- 分割线 ----------') pdf.close() # 保存Excel表到22.xlsx,直接替换,注意保存 endfile='22.xlsx' wb.save(endfile) print('\n') print('写入excel成功') print('保存位置:') print(os.getcwd()+"/"+endfile) print('\n') |