Python 如何在docx中读取和更新多个表?
我想更新word中多个表中第一列的单元格。我有一个在单个巨型表上工作的现有代码,现在同一个表正以不同的顺序拆分为多个表。我如何在多个具有相同内容的表上更新它? 这是用于更新一个表的代码的一部分Python 如何在docx中读取和更新多个表?,python,docx,python-docx,Python,Docx,Python Docx,我想更新word中多个表中第一列的单元格。我有一个在单个巨型表上工作的现有代码,现在同一个表正以不同的顺序拆分为多个表。我如何在多个具有相同内容的表上更新它? 这是用于更新一个表的代码的一部分 def update_list(self, save_path: str, list_docx: str = None): if list_docx is not None: self.list_docx = list_docx elif self.list_docx is
def update_list(self, save_path: str, list_docx: str = None):
if list_docx is not None:
self.list_docx = list_docx
elif self.list_docx is None:
raise ValueError('list is not given')
tracker = self.bill_compare()[0]
all_updates, removed = tracker.combine_found(), tracker.not_found_to_df()
docEdit = docEditor(self.list_docx)
self._updates_only(docEdit, all_updates)
self._removed_only(docEdit, removed)
docEdit.save(save_path)
def _updates_only(self, docEdit, all_updates):
for table in docEdit.tables:
for row in range(len(table.rows)):
partnum = table.get_text(row, 0)
if partnum in all_updates['old_partnum'].to_list():
to_update = all_updates.loc[pn == all_updates['old_partnum']].values[0]
# previous formatting
bold = table.isbold(row, 0)
# Update fields
self._update_partnum(row, to_update, table)
self._update_descript(row, to_update, table)
self._update_manufacturer(row, to_update, table)
self._update_model(row, to_update, table)
if bold:
table.bold_row(row)
self._match_conditions(row, table, to_update)
# Remove updated from df
all_updates = all_updates[all_updates.old_idx != to_update[0]]
class docEditor:
"""
Attributes:
document: the word document containing the list
table: the table within the word document containing the list
All functions require a row number for the row to be modified.
Funcitons ending with XXXX_row means the entire row will be modified,
while others will modify only modify one specific cell.
"""
def __init__(self, docx_path):
self.document = Document(docx_path)
self.table = self.document.tables[]
def get_text(self, row, column):
return self.table.rows[row].cells[column].text
def set_text(self, row, column, new_string):
self.table.rows[row].cells[column].text = new_string
def set_bold(self, row, column):
for paragraph in self.table.rows[row].cells[column].paragraphs:
for run in paragraph.runs:
run.font.bold = True
def bold_row(self, row):
for cell in self.table.rows[row].cells:
for paragraph in cell.paragraphs:
for run in paragraph.runs:
run.font.bold = True
def isbold(self, row, column):
for paragraph in self.table.rows[row].cells[column].paragraphs:
for run in paragraph.runs:
if run.font.bold:
return True
return False
def set_italic(self, row, column):
for paragraph in self.table.rows[row].cells[column].paragraphs:
for run in paragraph.runs:
run.font.italic = True
def italic_row(self, row):
for cell in self.table.rows[row].cells:
for paragraph in cell.paragraphs:
for run in paragraph.runs:
run.font.italic = True
def isitalic(self, row, column):
for paragraph in self.table.rows[row].cells[column].paragraphs:
for run in paragraph.runs:
if run.font.italic:
return True
return False
def set_highlight(self, row, column, colour):
for paragraph in self.table.rows[row].cells[column].paragraphs:
for run in paragraph.runs:
run.font.highlight_color = getattr(WD_COLOR_INDEX, colour)
def highlight_row(self, row, colour):
for cell in self.table.rows[row].cells:
for paragraph in cell.paragraphs:
for run in paragraph.runs:
run.font.highlight_color = getattr(WD_COLOR_INDEX, colour)
def set_colour(self, row, column, r, g, b):
for paragraph in self.table.rows[row].cells[column].paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(r, g, b)
def colour_row(self, row, r, g, b):
for cell in self.table.rows[row].cells:
for paragraph in cell.paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(r, g, b)
def get_font(self, row, column):
return self.table.rows[row].cells[column].paragraphs[0].runs[0].font.name
def set_font(self, row, column, font):
for paragraph in self.table.rows[row].cells[column].paragraphs:
for run in paragraph.runs:
run.font.name = font
def font_row(self, row, font):
for cell in self.table.rows[row].cells:
for paragraph in cell.paragraphs:
for run in paragraph.runs:
run.font.name = font
def set_fontsize(self, row, column, size):
for paragraph in self.table.rows[row].cells[column].paragraphs:
for run in paragraph.runs:
run.font.size = Pt(size)
def fontsize_row(self, row, size):
for cell in self.table.rows[row].cells:
for paragraph in cell.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(size)
def get_justification(self, row, column):
return self.table.rows[row].cells[column].paragraphs[0].alignment
def set_justification(self, row, column, justification):
# just = {'left': 0, 'center': 1, 'right': 3, 'distribute': 4}
for paragraph in self.table.rows[row].cells[column].paragraphs:
paragraph.alignment = justification
def set_strike(self, row, column):
for paragraph in self.table.rows[row].cells[column].paragraphs:
for run in paragraph.runs:
run.font.strike = True
def strike_row(self, row):
for cell in self.table.rows[row].cells:
for paragraph in cell.paragraphs:
for run in paragraph.runs:
run.font.strike = True
def save(self, path):
self.document.save(path)
你的问题是什么@Joaquin?此外,还需要修复代码的缩进。我分不清什么是内部循环,什么是外部循环。@很抱歉,我已经更新了缩进。我似乎无法更改此代码以更新同一个表,该表现在已拆分并以不同的顺序进行更新。我在docEdit.tables中添加了for表,以说明word文档中有多个表。docEdit设置为调用我创建的名为docEditor的类,该类更新表格的内容。太多代码未显示。就我所见,这里没有
pythondocx
代码。所有这些似乎都在您的docEditor
类中。很抱歉,如果看不到代码,就无法帮助您。@scanny我添加了那个类。希望这能提供更好的概述。Thanks@scanny我应该添加一个不同的循环还是遍历所有的表