extract_table.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import pandas as pd
  2. import pdfplumber
  3. import re
  4. from datetime import datetime
  5. import tabula
  6. ALLOWED_EXTENSIONS = {'pdf'}
  7. def allowed_file(filename):
  8. return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  9. def safe_filename(filename):
  10. """生成安全的文件名,同时保留中文"""
  11. # 保留中文、字母、数字、下划线和点
  12. keep_chars = (' ', '.', '_', '-')
  13. filename = "".join(c for c in filename if c.isalnum() or c in keep_chars).rstrip()
  14. return filename
  15. def get_pdf_page_count(pdf_path):
  16. with pdfplumber.open(pdf_path) as pdf:
  17. page_count = len(pdf.pages)
  18. return page_count
  19. def extract_temp_time(pdf_path):
  20. """第一种处理方法:基于文本分割的提取"""
  21. cleaned_data = []
  22. with pdfplumber.open(pdf_path) as pdf:
  23. for page in pdf.pages:
  24. text = page.extract_text()
  25. if text:
  26. text_list = text.split("\n")
  27. for txt in text_list:
  28. if ("历史数据表" not in txt) and ("时间" not in txt):
  29. foo = [p for p in re.split(r'\s{1,}', txt.strip()) if p]
  30. if len(foo) < 5:
  31. print(foo)
  32. continue
  33. date_time, name, ids, temp, humi = foo[0] + " " + foo[1], foo[2], foo[3], foo[4], foo[5]
  34. if foo[5] == "--":
  35. humi = ""
  36. cleaned_data.append([date_time, name, ids, temp, humi])
  37. df = pd.DataFrame(
  38. cleaned_data,
  39. columns=['时间', '名称', '编号', '温度', '湿度']
  40. )
  41. df = df.sort_values('时间').reset_index(drop=True)
  42. return df
  43. def extract_pdf_table_to_excel(pdf_path):
  44. """第二种处理方法:基于表格提取"""
  45. cleaned_data = []
  46. with pdfplumber.open(pdf_path) as pdf:
  47. for page in pdf.pages[2:]:
  48. tables = page.extract_table()
  49. if tables:
  50. if len(tables) >= 2:
  51. for table in tables[1:]:
  52. for row in table:
  53. for cell in row.split('\n'):
  54. foo = str(cell).strip().split(" ")
  55. if len(foo) == 4:
  56. date_time, temp, humi = foo[0].replace("/", "-") + " " + foo[1], foo[2], foo[3]
  57. # 拆分日期和时间
  58. cleaned_data.append([date_time, temp, humi])
  59. result_df = pd.DataFrame(
  60. cleaned_data,
  61. columns=['时间', '温度', '湿度']
  62. )
  63. result_df = result_df.sort_values('时间').reset_index(drop=True)
  64. return result_df
  65. def extract_temp_by_datetime_pattern(pdf_path):
  66. """第三种处理方法:基于日期时间模式和温度符号的提取"""
  67. all_data = []
  68. datetime_pattern = re.compile(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}')
  69. with pdfplumber.open(pdf_path) as pdf:
  70. # 从第二页开始处理(索引1)
  71. for page in pdf.pages[1:]:
  72. text = page.extract_text()
  73. if not text:
  74. continue
  75. lines = text.split('\n')
  76. for line in lines:
  77. # 检查行是否包含日期时间格式和温度符号
  78. if datetime_pattern.search(line) and '℃' in line:
  79. parts = line.split()
  80. if len(parts) >= 3:
  81. # 提取时间部分
  82. time_str = ' '.join(parts[:2])
  83. try:
  84. # 转换为datetime对象
  85. time = datetime.strptime(time_str, '%Y-%m-%d %H:%M')
  86. # 提取温度值(去掉℃符号)
  87. temp_str = parts[2].replace('℃', '')
  88. try:
  89. # 添加到数据列表
  90. all_data.append({'时间': time, '温度': temp_str})
  91. except ValueError:
  92. continue
  93. except ValueError:
  94. continue
  95. if len(parts) >= 6:
  96. # 提取时间部分
  97. time_str = ' '.join(parts[3:5])
  98. try:
  99. # 转换为datetime对象
  100. time = datetime.strptime(time_str, '%Y-%m-%d %H:%M')
  101. # 提取温度值(去掉℃符号)
  102. temp_str = parts[5].replace('℃', '')
  103. try:
  104. # 添加到数据列表
  105. all_data.append({'时间': time, '温度': temp_str})
  106. except ValueError:
  107. continue
  108. except ValueError:
  109. continue
  110. df = pd.DataFrame(all_data, columns=['时间', '温度'])
  111. df = df.sort_values('时间').reset_index(drop=True)
  112. return df
  113. def extract_temperature_data_from_pdf(pdf_path):
  114. """
  115. 从PDF文件中提取时间和温度数据
  116. """
  117. all_data = []
  118. with pdfplumber.open(pdf_path) as pdf:
  119. for page in pdf.pages:
  120. text = page.extract_text()
  121. # 使用正则表达式匹配数据行
  122. # 匹配模式: 序号 | 日期时间 | 温度 | 状态
  123. for value in text.split("\n"):
  124. pattern = r'(\d+)\s+(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\s+(-?\d+\.\d+|-?\d+)\s+([^\s]+)'
  125. matches = re.findall(pattern, value)
  126. for match in matches:
  127. index, datetime_str, temperature, status = match
  128. all_data.append({
  129. '时间': datetime_str,
  130. '温度': temperature,
  131. })
  132. # 转换为DataFrame
  133. df = pd.DataFrame(all_data, columns=['时间', '温度'])
  134. # 按时间排序
  135. df = df.sort_values('时间').reset_index(drop=True)
  136. return df
  137. # 第五种处理方法
  138. def extract_data_from_pdf_5(pdf_path):
  139. """
  140. 从PDF文件中提取时间和温度数据
  141. """
  142. cleaned_data = []
  143. valid_table_lines = [] # 存储目标文件中有效表格行
  144. with pdfplumber.open(pdf_path) as pdf:
  145. for page in pdf.pages:
  146. page_content = page.extract_text()
  147. if not page_content:
  148. continue # 跳过空页
  149. # 按换行符分割为单行,去除首尾空格,排除页码行(如“第1页/共3页”)
  150. page_lines = [
  151. line.strip() for line in page_content.split("\n")
  152. if "第" not in line or "页" not in line
  153. ]
  154. for line in page_lines:
  155. if '时间' in line:
  156. continue
  157. # 筛选条件:含时间(任意年月日时分)+ 温度(数字℃)+ 湿度(数字%)特征
  158. has_time = re.search(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}", line)
  159. has_temp = re.search(r"\d+\.?\d*℃", line)
  160. has_humi = re.search(r"\d+\.?\d*%", line)
  161. if has_time and has_temp:
  162. valid_table_lines.append(line)
  163. for line_idx, line_content in enumerate(valid_table_lines, 1):
  164. parts = line_content.split(" ")
  165. if len(parts) == 4:
  166. data = {
  167. "时间": parts[0] + " " + parts[1],
  168. "温度": parts[2].replace("℃", ""),
  169. }
  170. cleaned_data.append(data)
  171. if len(parts) == 8:
  172. data1 = {
  173. "时间": parts[0] + " " + parts[1],
  174. "温度": parts[2].replace("℃", ""),
  175. }
  176. data2 = {
  177. "时间": parts[4] + " " + parts[5],
  178. "温度": parts[6].replace("℃", ""),
  179. }
  180. cleaned_data.append(data1)
  181. cleaned_data.append(data2)
  182. if len(parts) == 6:
  183. data = {
  184. "时间": parts[0] + " " + parts[1],
  185. "温度": parts[2].replace("℃", ""),
  186. "湿度": parts[4].replace("%", ""),
  187. }
  188. cleaned_data.append(data)
  189. if len(parts) == 12:
  190. data1 = {
  191. "时间": parts[0] + " " + parts[1],
  192. "温度": parts[2].replace("℃", ""),
  193. "湿度": parts[4].replace("%", ""),
  194. }
  195. data2 = {
  196. "时间": parts[6] + " " + parts[7],
  197. "温度": parts[8].replace("℃", ""),
  198. "湿度": parts[10].replace("%", ""),
  199. }
  200. cleaned_data.append(data1)
  201. cleaned_data.append(data2)
  202. # 转换为DataFrame
  203. df = pd.DataFrame(cleaned_data, columns=['时间', '温度', '湿度'])
  204. # 按时间排序
  205. df = df.sort_values('时间').reset_index(drop=True)
  206. return df