Skip to content

Instantly share code, notes, and snippets.

@ssz66666
Created June 21, 2019 19:11
Show Gist options
  • Save ssz66666/fb9589e5d25be374217119fb7a23e054 to your computer and use it in GitHub Desktop.
Save ssz66666/fb9589e5d25be374217119fb7a23e054 to your computer and use it in GitHub Desktop.
把石墨评论导入word
bayoo-docx==0.2.0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import docx
import re
with open('chapter2-comments.txt', 'r', encoding='utf-8') as f:
commtext = json.load(f)
with open('maintext-dedup.txt', 'r', encoding='utf-8') as f:
maintext = json.load(f)
document = docx.Document("modified.docx")
comment_refs = []
comment_pattern = re.compile(r'\"(comment-\w+)\"')
comments = {}
for cmt in commtext:
guid = cmt['selection_guid']
if guid in comments:
comments[guid].append(cmt['content'].strip())
else:
comments[guid] = [cmt['content'].strip()]
for mtxt in maintext:
if len(mtxt) == 3:
match = comment_pattern.search(mtxt[2])
if match is not None:
comment_refs.append((match[1], mtxt[1].strip()))
i = 0
j = 0
paras = document.paragraphs
while True:
cmt = comment_refs[j]
p = paras[i]
matching = re.search(re.escape(cmt[1]), p.text)
if matching is None:
i = i + 1
if i >= len(paras):
print(cmt[1])
print("selection", cmt[1], "is never found!")
raise ValueError()
else:
print("found selection", cmt[1], "at paragraph", i)
for cmt_text in comments[cmt[0]]:
p.add_comment(cmt_text)
j = j + 1
if j >= len(comment_refs):
print("finished!")
break
document.save('modified.docx')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment