我对Python有点陌生,很难从.docx文件中的表中获取粗体文本。
我知道如何将表格中的所有信息添加到列表中:
document = Document('path_to_the_.docx_ file')
document.save('path_to_the_.docx_ file')
tables = []
for table in document.tables:
for row in table.rows:
for cell in row.cells:
for para in cell.paragraphs:
tables.append(para.text)
tables
我知道如何获得不在表格中的粗体文本:
document = Document('path_to_the_.docx_ file')
for paragraph in document.paragraphs:
for run in paragraph.runs:
if run.bold:
print(run.text)
请帮我从表格中获取粗体文本。
提前感谢!
以下是存储在.docx文件的某个表中的信息示例:
Bla1 Bla1 Bla1–的共同所有者、总裁、董事会主席
Bla1先生是一位知名的俄罗斯企业家,他的商业兴趣和职业生涯主要与IT、营销、广告和咨询服务行业有关。
Bla2 Bla2–总经理,首席执行官报告
bla2-是一位中等知名度的德国个人,他的职业生涯主要与市场营销、汽车、消费品、食品制造和贸易部门有关。根据公开资料,在1994-2005年,他是一名高级工程师。。。
Bla3 Bla3 Bla3–财务总监
Bla4 Bla4–总会计师
根据本报告要求者的规定,目标公司的总会计师还负责一般归属于首席财务官的职能,例如管理财务和财务风险以及财务规划。
所以我只想得到单词:Bla1 Bla1 Bla1,Bra2 Bla2、Bla3 Bla3和布拉4 Bla4因为这些单词是唯一的bold
from docx import Document
document = Document('test.docx')
if document.tables:
table = document.tables[0]
for row_index, row in enumerate(table.rows):
for cell_index, cell in enumerate(row.cells):
for paragraph in cell.paragraphs:
for run in paragraph.runs:
if run.bold:
print(run.text)
from docx import Document
document = Document('test.docx')
p1 = document.add_paragraph()
p1.add_run("Bla1 bla1 bla1").bold=True
p1.add_run(' - co-owner, president, reported chairman of the board of directors')
p2 = document.add_paragraph('Mr Bla1 is a high-profile Russian entrepreneur, whose business interests and career has been primarily associated with the IT, marketing, advertising and consulting services sectors.')
p1 = document.add_paragraph()
p1.add_run('Bla2 bla2').bold=True
p1.add_run(' - general director, reported chief executive officer')
p3 = document.add_paragraph('Mr bla2 - is a medium-profile German individual, whose career has been primarily associated with the marketing, as well as car, consumers goods, and food manufacturing and trading sectors. According to publicly available sources, in 1994-2005 he was a senior engineer...')
p4 = document.add_paragraph()
p4.add_run('Bla3 bla3 bla3').bold=True
p4.add_run(' - financial director')
p5 = document.add_paragraph()
p5.add_run('Bla4 bla4 ').bold=True
p5.add_run('- chief accountant')
p6 = document.add_paragraph("As provided by the requestor of this report, the Target's chief accountant is also in charge for functions attributed in general to chief financial officer, e.g. managing the finances and financial risks as well as financial planning.")
document.save('test.docx')