从Google文档中提取信息到Pandas数据框。

huangapple go评论99阅读模式
英文:

Extract information from Google Doc into Pandas DF

问题

我正在寻找一种方法从我的私人Google文档中提取信息并将它们放入DF(数据框)以执行一些简单的分析。

我已连接到Google文档API,并能够读取文档,但我正在努力提取仅相关信息并按正确顺序排列。

我的文档如下所示:
从Google文档中提取信息到Pandas数据框。

我需要的输出如下所示:
从Google文档中提取信息到Pandas数据框。

我可以创建单个项目的正则表达式,但如何将它们分组到像上面那样的表中,考虑到单个日期的段落数和标签数可能会有所变化?

谢谢!

这是我从API收到的内容:

  1. {'title': 'test',
  2. 'body': {'content': [{'endIndex': 1,
  3. 'sectionBreak': {'sectionStyle': {'columnSeparatorStyle': 'NONE',
  4. 'contentDirection': 'LEFT_TO_RIGHT',
  5. 'sectionType': 'CONTINUOUS'}}},
  6. {'startIndex': 1,
  7. 'endIndex': 11,
  8. 'paragraph': {'elements': [{'startIndex': 1,
  9. 'endIndex': 11,
  10. 'textRun': {'content': 'Doc Title\n',
  11. 'textStyle': {'bold': True,
  12. 'fontSize': {'magnitude': 13, 'unit': 'PT'}}}}],
  13. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  14. 'alignment': 'CENTER',
  15. 'direction': 'LEFT_TO_RIGHT'}}},
  16. {'startIndex': 11,
  17. 'endIndex': 12,
  18. 'paragraph': {'elements': [{'startIndex': 11,
  19. 'endIndex': 12,
  20. 'textRun': {'content': '\n',
  21. 'textStyle': {'bold': True,
  22. 'fontSize': {'magnitude': 13, 'unit': 'PT'}}}}],
  23. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  24. 'alignment': 'CENTER',
  25. 'direction': 'LEFT_TO_RIGHT'}}},
  26. ...
  27. ...

请注意,以上只是给出的一部分内容,其中包含了文档的结构和示例文本段。如果您需要对文档的特定部分进行进一步的翻译或解释,请提供详细的信息。

英文:

I'm looking for a way to extract information from my private Google Doc and put them into DF to perform some simple analysis.

I'm connected to the Google Doc API and able to read the document but I'm struggling with extracting only relevant information and in correct order.

This is how my doc looks like:
从Google文档中提取信息到Pandas数据框。

And this is the output I need to have:

从Google文档中提取信息到Pandas数据框。

I would be able to create separate regexes for single items but how to group it into table like above, given that number of paragraphs for single date can vary (same for tags)?

Thanks

This is what I received from API:

  1. {'title': 'test',
  2. 'body': {'content': [{'endIndex': 1,
  3. 'sectionBreak': {'sectionStyle': {'columnSeparatorStyle': 'NONE',
  4. 'contentDirection': 'LEFT_TO_RIGHT',
  5. 'sectionType': 'CONTINUOUS'}}},
  6. {'startIndex': 1,
  7. 'endIndex': 11,
  8. 'paragraph': {'elements': [{'startIndex': 1,
  9. 'endIndex': 11,
  10. 'textRun': {'content': 'Doc Title\n',
  11. 'textStyle': {'bold': True,
  12. 'fontSize': {'magnitude': 13, 'unit': 'PT'}}}}],
  13. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  14. 'alignment': 'CENTER',
  15. 'direction': 'LEFT_TO_RIGHT'}}},
  16. {'startIndex': 11,
  17. 'endIndex': 12,
  18. 'paragraph': {'elements': [{'startIndex': 11,
  19. 'endIndex': 12,
  20. 'textRun': {'content': '\n',
  21. 'textStyle': {'bold': True,
  22. 'fontSize': {'magnitude': 13, 'unit': 'PT'}}}}],
  23. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  24. 'alignment': 'CENTER',
  25. 'direction': 'LEFT_TO_RIGHT'}}},
  26. {'startIndex': 12,
  27. 'endIndex': 13,
  28. 'paragraph': {'elements': [{'startIndex': 12,
  29. 'endIndex': 13,
  30. 'textRun': {'content': '\n',
  31. 'textStyle': {'bold': True,
  32. 'fontSize': {'magnitude': 13, 'unit': 'PT'}}}}],
  33. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  34. 'alignment': 'CENTER',
  35. 'direction': 'LEFT_TO_RIGHT'}}},
  36. {'startIndex': 13,
  37. 'endIndex': 23,
  38. 'paragraph': {'elements': [{'startIndex': 13,
  39. 'endIndex': 23,
  40. 'textRun': {'content': '7.03.2023\n',
  41. 'textStyle': {'bold': True,
  42. 'fontSize': {'magnitude': 13, 'unit': 'PT'}}}}],
  43. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  44. 'alignment': 'CENTER',
  45. 'direction': 'LEFT_TO_RIGHT'}}},
  46. {'startIndex': 23,
  47. 'endIndex': 24,
  48. 'paragraph': {'elements': [{'startIndex': 23,
  49. 'endIndex': 24,
  50. 'textRun': {'content': '\n',
  51. 'textStyle': {'bold': True,
  52. 'fontSize': {'magnitude': 13, 'unit': 'PT'}}}}],
  53. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  54. 'alignment': 'CENTER',
  55. 'direction': 'LEFT_TO_RIGHT'}}},
  56. {'startIndex': 24,
  57. 'endIndex': 27,
  58. 'paragraph': {'elements': [{'startIndex': 24,
  59. 'endIndex': 27,
  60. 'textRun': {'content': '1.\n',
  61. 'textStyle': {'bold': True,
  62. 'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  63. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  64. 'direction': 'LEFT_TO_RIGHT'}}},
  65. {'startIndex': 27,
  66. 'endIndex': 41,
  67. 'paragraph': {'elements': [{'startIndex': 27,
  68. 'endIndex': 41,
  69. 'textRun': {'content': 'Example text\n',
  70. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  71. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  72. 'direction': 'LEFT_TO_RIGHT'}}},
  73. {'startIndex': 41,
  74. 'endIndex': 42,
  75. 'paragraph': {'elements': [{'startIndex': 41,
  76. 'endIndex': 42,
  77. 'textRun': {'content': '\n',
  78. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  79. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  80. 'direction': 'LEFT_TO_RIGHT'}}},
  81. {'startIndex': 42,
  82. 'endIndex': 55,
  83. 'paragraph': {'elements': [{'startIndex': 42,
  84. 'endIndex': 55,
  85. 'textRun': {'content': '#tag1, #tag2\n',
  86. 'textStyle': {'underline': True,
  87. 'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  88. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  89. 'direction': 'LEFT_TO_RIGHT'}}},
  90. {'startIndex': 55,
  91. 'endIndex': 56,
  92. 'paragraph': {'elements': [{'startIndex': 55,
  93. 'endIndex': 56,
  94. 'textRun': {'content': '\n',
  95. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  96. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  97. 'direction': 'LEFT_TO_RIGHT'}}},
  98. {'startIndex': 56,
  99. 'endIndex': 59,
  100. 'paragraph': {'elements': [{'startIndex': 56,
  101. 'endIndex': 59,
  102. 'textRun': {'content': '2.\n',
  103. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  104. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  105. 'direction': 'LEFT_TO_RIGHT'}}},
  106. {'startIndex': 59,
  107. 'endIndex': 73,
  108. 'paragraph': {'elements': [{'startIndex': 59,
  109. 'endIndex': 73,
  110. 'textRun': {'content': 'Example text\n',
  111. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  112. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  113. 'direction': 'LEFT_TO_RIGHT'}}},
  114. {'startIndex': 73,
  115. 'endIndex': 74,
  116. 'paragraph': {'elements': [{'startIndex': 73,
  117. 'endIndex': 74,
  118. 'textRun': {'content': '\n',
  119. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  120. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  121. 'direction': 'LEFT_TO_RIGHT'}}},
  122. {'startIndex': 74,
  123. 'endIndex': 94,
  124. 'paragraph': {'elements': [{'startIndex': 74,
  125. 'endIndex': 94,
  126. 'textRun': {'content': '#tag1, #tag2, #tag3\n',
  127. 'textStyle': {'underline': True,
  128. 'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  129. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  130. 'direction': 'LEFT_TO_RIGHT'}}},
  131. {'startIndex': 94,
  132. 'endIndex': 95,
  133. 'paragraph': {'elements': [{'startIndex': 94,
  134. 'endIndex': 95,
  135. 'textRun': {'content': '\n',
  136. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  137. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  138. 'direction': 'LEFT_TO_RIGHT'}}},
  139. {'startIndex': 95,
  140. 'endIndex': 105,
  141. 'paragraph': {'elements': [{'startIndex': 95,
  142. 'endIndex': 105,
  143. 'textRun': {'content': '8.03.2023\n',
  144. 'textStyle': {'bold': True,
  145. 'fontSize': {'magnitude': 13, 'unit': 'PT'}}}}],
  146. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  147. 'alignment': 'CENTER',
  148. 'direction': 'LEFT_TO_RIGHT'}}},
  149. {'startIndex': 105,
  150. 'endIndex': 106,
  151. 'paragraph': {'elements': [{'startIndex': 105,
  152. 'endIndex': 106,
  153. 'textRun': {'content': '\n',
  154. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  155. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  156. 'direction': 'LEFT_TO_RIGHT'}}},
  157. {'startIndex': 106,
  158. 'endIndex': 109,
  159. 'paragraph': {'elements': [{'startIndex': 106,
  160. 'endIndex': 109,
  161. 'textRun': {'content': '1.\n',
  162. 'textStyle': {'bold': True,
  163. 'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  164. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  165. 'direction': 'LEFT_TO_RIGHT'}}},
  166. {'startIndex': 109,
  167. 'endIndex': 123,
  168. 'paragraph': {'elements': [{'startIndex': 109,
  169. 'endIndex': 123,
  170. 'textRun': {'content': 'Example text\n',
  171. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  172. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  173. 'direction': 'LEFT_TO_RIGHT'}}},
  174. {'startIndex': 123,
  175. 'endIndex': 124,
  176. 'paragraph': {'elements': [{'startIndex': 123,
  177. 'endIndex': 124,
  178. 'textRun': {'content': '\n',
  179. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  180. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  181. 'direction': 'LEFT_TO_RIGHT'}}},
  182. {'startIndex': 124,
  183. 'endIndex': 137,
  184. 'paragraph': {'elements': [{'startIndex': 124,
  185. 'endIndex': 137,
  186. 'textRun': {'content': '#tag1, #tag2\n',
  187. 'textStyle': {'underline': True,
  188. 'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  189. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  190. 'direction': 'LEFT_TO_RIGHT'}}},
  191. {'startIndex': 137,
  192. 'endIndex': 138,
  193. 'paragraph': {'elements': [{'startIndex': 137,
  194. 'endIndex': 138,
  195. 'textRun': {'content': '\n',
  196. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  197. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  198. 'direction': 'LEFT_TO_RIGHT'}}},
  199. {'startIndex': 138,
  200. 'endIndex': 141,
  201. 'paragraph': {'elements': [{'startIndex': 138,
  202. 'endIndex': 141,
  203. 'textRun': {'content': '2.\n',
  204. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  205. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  206. 'direction': 'LEFT_TO_RIGHT'}}},
  207. {'startIndex': 141,
  208. 'endIndex': 155,
  209. 'paragraph': {'elements': [{'startIndex': 141,
  210. 'endIndex': 155,
  211. 'textRun': {'content': 'Example text\n',
  212. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  213. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  214. 'direction': 'LEFT_TO_RIGHT'}}},
  215. {'startIndex': 155,
  216. 'endIndex': 156,
  217. 'paragraph': {'elements': [{'startIndex': 155,
  218. 'endIndex': 156,
  219. 'textRun': {'content': '\n',
  220. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  221. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  222. 'direction': 'LEFT_TO_RIGHT'}}},
  223. {'startIndex': 156,
  224. 'endIndex': 176,
  225. 'paragraph': {'elements': [{'startIndex': 156,
  226. 'endIndex': 176,
  227. 'textRun': {'content': '#tag1, #tag2, #tag3\n',
  228. 'textStyle': {'underline': True,
  229. 'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  230. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  231. 'direction': 'LEFT_TO_RIGHT'}}},
  232. {'startIndex': 176,
  233. 'endIndex': 177,
  234. 'paragraph': {'elements': [{'startIndex': 176,
  235. 'endIndex': 177,
  236. 'textRun': {'content': '\n',
  237. 'textStyle': {'underline': True,
  238. 'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  239. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  240. 'direction': 'LEFT_TO_RIGHT'}}},
  241. {'startIndex': 177,
  242. 'endIndex': 180,
  243. 'paragraph': {'elements': [{'startIndex': 177,
  244. 'endIndex': 180,
  245. 'textRun': {'content': '3.\n',
  246. 'textStyle': {'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  247. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  248. 'direction': 'LEFT_TO_RIGHT'}}},
  249. {'startIndex': 180,
  250. 'endIndex': 186,
  251. 'paragraph': {'elements': [{'startIndex': 180,
  252. 'endIndex': 186,
  253. 'textRun': {'content': '#tag1\n',
  254. 'textStyle': {'underline': True,
  255. 'fontSize': {'magnitude': 10, 'unit': 'PT'}}}}],
  256. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  257. 'direction': 'LEFT_TO_RIGHT'}}}]},
  258. 'documentStyle': {'background': {'color': {}},
  259. 'pageNumberStart': 1,
  260. 'marginTop': {'magnitude': 72, 'unit': 'PT'},
  261. 'marginBottom': {'magnitude': 72, 'unit': 'PT'},
  262. 'marginRight': {'magnitude': 72, 'unit': 'PT'},
  263. 'marginLeft': {'magnitude': 72, 'unit': 'PT'},
  264. 'pageSize': {'height': {'magnitude': 841.8897637795277, 'unit': 'PT'},
  265. 'width': {'magnitude': 595.2755905511812, 'unit': 'PT'}},
  266. 'marginHeader': {'magnitude': 36, 'unit': 'PT'},
  267. 'marginFooter': {'magnitude': 36, 'unit': 'PT'},
  268. 'useCustomHeaderFooterMargins': True},
  269. 'namedStyles': {'styles': [{'namedStyleType': 'NORMAL_TEXT',
  270. 'textStyle': {'bold': False,
  271. 'italic': False,
  272. 'underline': False,
  273. 'strikethrough': False,
  274. 'smallCaps': False,
  275. 'backgroundColor': {},
  276. 'foregroundColor': {'color': {'rgbColor': {}}},
  277. 'fontSize': {'magnitude': 11, 'unit': 'PT'},
  278. 'weightedFontFamily': {'fontFamily': 'Arial', 'weight': 400},
  279. 'baselineOffset': 'NONE'},
  280. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  281. 'alignment': 'START',
  282. 'lineSpacing': 115,
  283. 'direction': 'LEFT_TO_RIGHT',
  284. 'spacingMode': 'COLLAPSE_LISTS',
  285. 'spaceAbove': {'unit': 'PT'},
  286. 'spaceBelow': {'unit': 'PT'},
  287. 'borderBetween': {'color': {},
  288. 'width': {'unit': 'PT'},
  289. 'padding': {'unit': 'PT'},
  290. 'dashStyle': 'SOLID'},
  291. 'borderTop': {'color': {},
  292. 'width': {'unit': 'PT'},
  293. 'padding': {'unit': 'PT'},
  294. 'dashStyle': 'SOLID'},
  295. 'borderBottom': {'color': {},
  296. 'width': {'unit': 'PT'},
  297. 'padding': {'unit': 'PT'},
  298. 'dashStyle': 'SOLID'},
  299. 'borderLeft': {'color': {},
  300. 'width': {'unit': 'PT'},
  301. 'padding': {'unit': 'PT'},
  302. 'dashStyle': 'SOLID'},
  303. 'borderRight': {'color': {},
  304. 'width': {'unit': 'PT'},
  305. 'padding': {'unit': 'PT'},
  306. 'dashStyle': 'SOLID'},
  307. 'indentFirstLine': {'unit': 'PT'},
  308. 'indentStart': {'unit': 'PT'},
  309. 'indentEnd': {'unit': 'PT'},
  310. 'keepLinesTogether': False,
  311. 'keepWithNext': False,
  312. 'avoidWidowAndOrphan': True,
  313. 'shading': {'backgroundColor': {}},
  314. 'pageBreakBefore': False}},
  315. {'namedStyleType': 'HEADING_1',
  316. 'textStyle': {'fontSize': {'magnitude': 20, 'unit': 'PT'}},
  317. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  318. 'direction': 'LEFT_TO_RIGHT',
  319. 'spaceAbove': {'magnitude': 20, 'unit': 'PT'},
  320. 'spaceBelow': {'magnitude': 6, 'unit': 'PT'},
  321. 'keepLinesTogether': True,
  322. 'keepWithNext': True,
  323. 'pageBreakBefore': False}},
  324. {'namedStyleType': 'HEADING_2',
  325. 'textStyle': {'bold': False, 'fontSize': {'magnitude': 16, 'unit': 'PT'}},
  326. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  327. 'direction': 'LEFT_TO_RIGHT',
  328. 'spaceAbove': {'magnitude': 18, 'unit': 'PT'},
  329. 'spaceBelow': {'magnitude': 6, 'unit': 'PT'},
  330. 'keepLinesTogether': True,
  331. 'keepWithNext': True,
  332. 'pageBreakBefore': False}},
  333. {'namedStyleType': 'HEADING_3',
  334. 'textStyle': {'bold': False,
  335. 'foregroundColor': {'color': {'rgbColor': {'red': 0.2627451,
  336. 'green': 0.2627451,
  337. 'blue': 0.2627451}}},
  338. 'fontSize': {'magnitude': 14, 'unit': 'PT'}},
  339. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  340. 'direction': 'LEFT_TO_RIGHT',
  341. 'spaceAbove': {'magnitude': 16, 'unit': 'PT'},
  342. 'spaceBelow': {'magnitude': 4, 'unit': 'PT'},
  343. 'keepLinesTogether': True,
  344. 'keepWithNext': True,
  345. 'pageBreakBefore': False}},
  346. {'namedStyleType': 'HEADING_4',
  347. 'textStyle': {'foregroundColor': {'color': {'rgbColor': {'red': 0.4,
  348. 'green': 0.4,
  349. 'blue': 0.4}}},
  350. 'fontSize': {'magnitude': 12, 'unit': 'PT'}},
  351. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  352. 'direction': 'LEFT_TO_RIGHT',
  353. 'spaceAbove': {'magnitude': 14, 'unit': 'PT'},
  354. 'spaceBelow': {'magnitude': 4, 'unit': 'PT'},
  355. 'keepLinesTogether': True,
  356. 'keepWithNext': True,
  357. 'pageBreakBefore': False}},
  358. {'namedStyleType': 'HEADING_5',
  359. 'textStyle': {'foregroundColor': {'color': {'rgbColor': {'red': 0.4,
  360. 'green': 0.4,
  361. 'blue': 0.4}}},
  362. 'fontSize': {'magnitude': 11, 'unit': 'PT'}},
  363. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  364. 'direction': 'LEFT_TO_RIGHT',
  365. 'spaceAbove': {'magnitude': 12, 'unit': 'PT'},
  366. 'spaceBelow': {'magnitude': 4, 'unit': 'PT'},
  367. 'keepLinesTogether': True,
  368. 'keepWithNext': True,
  369. 'pageBreakBefore': False}},
  370. {'namedStyleType': 'HEADING_6',
  371. 'textStyle': {'italic': True,
  372. 'foregroundColor': {'color': {'rgbColor': {'red': 0.4,
  373. 'green': 0.4,
  374. 'blue': 0.4}}},
  375. 'fontSize': {'magnitude': 11, 'unit': 'PT'}},
  376. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  377. 'direction': 'LEFT_TO_RIGHT',
  378. 'spaceAbove': {'magnitude': 12, 'unit': 'PT'},
  379. 'spaceBelow': {'magnitude': 4, 'unit': 'PT'},
  380. 'keepLinesTogether': True,
  381. 'keepWithNext': True,
  382. 'pageBreakBefore': False}},
  383. {'namedStyleType': 'TITLE',
  384. 'textStyle': {'fontSize': {'magnitude': 26, 'unit': 'PT'}},
  385. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  386. 'direction': 'LEFT_TO_RIGHT',
  387. 'spaceAbove': {'unit': 'PT'},
  388. 'spaceBelow': {'magnitude': 3, 'unit': 'PT'},
  389. 'keepLinesTogether': True,
  390. 'keepWithNext': True,
  391. 'pageBreakBefore': False}},
  392. {'namedStyleType': 'SUBTITLE',
  393. 'textStyle': {'italic': False,
  394. 'foregroundColor': {'color': {'rgbColor': {'red': 0.4,
  395. 'green': 0.4,
  396. 'blue': 0.4}}},
  397. 'fontSize': {'magnitude': 15, 'unit': 'PT'},
  398. 'weightedFontFamily': {'fontFamily': 'Arial', 'weight': 400}},
  399. 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT',
  400. 'direction': 'LEFT_TO_RIGHT',
  401. 'spaceAbove': {'unit': 'PT'},
  402. 'spaceBelow': {'magnitude': 16, 'unit': 'PT'},
  403. 'keepLinesTogether': True,
  404. 'keepWithNext': True,
  405. 'pageBreakBefore': False}}]},
  406. 'revisionId': 'xxxxxxx',
  407. 'suggestionsViewMode': 'SUGGESTIONS_INLINE',
  408. 'documentId': 'xxxxxxx'}

答案1

得分: 3

假设 d 是由Google API返回的字典,您可以使用以下代码:

  1. import itertools
  2. # 提取内容数据
  3. content = itertools.chain(*[c['paragraph']['elements']
  4. for c in d['body']['content']
  5. if 'paragraph' in c])
  6. # 提取期望数据
  7. df = (pd.json_normalize(content)['textRun.content']
  8. .str.strip().loc[lambda x: x.str.len() > 0]
  9. .str.extract('^(?P<Date>\d+\.\d+\.\d+)|(?P<Paragraph>\d+\.)|(?P<Tags>#.*)')
  10. .dropna(how='all'))
  11. # 过滤数据框
  12. out = (df.ffill().loc[df['Tags'].notna()]
  13. .assign(Tags=lambda x: x['Tags'].str.split(',\s*'))
  14. .reset_index(drop=True))

输出:

  1. >>> out
  2. Date Paragraph Tags
  3. 0 7.03.2023 1. [#tag1, #tag2]
  4. 1 7.03.2023 2. [#tag1, #tag2, #tag3]
  5. 2 8.03.2023 1. [#tag1, #tag2]
  6. 3 8.03.2023 2. [#tag1, #tag2, #tag3]
  7. 4 8.03.2023 3. [#tag1]
英文:

Suppose d the dictionary returned by Google API, you can use:

  1. import itertools
  2. # Extract content data
  3. content = itertools.chain(*[c[&#39;paragraph&#39;][&#39;elements&#39;]
  4. for c in d[&#39;body&#39;][&#39;content&#39;]
  5. if &#39;paragraph&#39; in c])
  6. # Extract expected data
  7. df = (pd.json_normalize(content)[&#39;textRun.content&#39;]
  8. .str.strip().loc[lambda x: x.str.len() &gt; 0]
  9. .str.extract(&#39;^(?P&lt;Date&gt;\d+\.\d+\.\d+)|(?P&lt;Paragraph&gt;\d+\.)|(?P&lt;Tags&gt;#.*)&#39;)
  10. .dropna(how=&#39;all&#39;))
  11. # Filter dataframe
  12. out = (df.ffill().loc[df[&#39;Tags&#39;].notna()]
  13. .assign(Tags=lambda x: x[&#39;Tags&#39;].str.split(&#39;,\s*&#39;))
  14. .reset_index(drop=True))

Output:

  1. &gt;&gt;&gt; out
  2. Date Paragraph Tags
  3. 0 7.03.2023 1. [#tag1, #tag2]
  4. 1 7.03.2023 2. [#tag1, #tag2, #tag3]
  5. 2 8.03.2023 1. [#tag1, #tag2]
  6. 3 8.03.2023 2. [#tag1, #tag2, #tag3]
  7. 4 8.03.2023 3. [#tag1]

huangapple
  • 本文由 发表于 2023年3月7日 21:23:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定