学的是b站的课程(千锋教育),跟老师写程序,不是自创的代码!
今天是学Python的第26天,学的内容是运用Python在Word中插入列表和表格,以及读取docx文件。开学了,时间不多,写得不多,见谅。
from docx import Document
语法:docx.Document()
document = docx.Document()
语法:document.add_paragraph(列表内容,列表样式)
在下面的代码中,我们使用style = 'List Bullet'来指定段落的样式为无序列表项。
# 添加无序列表
document.add_paragraph('第一步:hello',style='List Bullet')
document.add_paragraph('第二步:world',style='List Bullet')
也可以通过循环来添加多个无序列表项.
items = ['Apple', 'Banana', 'Orange']
for item in items:
document.add_paragraph(item, style='List Bullet')
语法:document.add_paragraph(列表内容,列表样式)
在下面的代码中,我们使用style = 'List Number'来指定段落的样式为无序列表项。
document.add_paragraph('刘备',style='List Number')
document.add_paragraph('关羽',style='List Number')
document.add_paragraph('张飞',style='List Number')
也可以通过循环来添加多个有序列表项.
items = ['First step', 'Second step', 'Third step']
for index, item in enumerate(items, start=1):
document.add_paragraph(f'{index}. {item}', style='List Number')
语法:document.save()
document.save('new_document.docx')
from docx import Document
# 列表分为有序列表和无序列表(序号)
# 创建word文档对象
document = Document()
# 添加无序列表
document.add_paragraph('第一步:hello',style='List Bullet')
document.add_paragraph('第二步:world',style='List Bullet')
# 通过循环来添加多个无序列表项
items = ['Apple', 'Banana', 'Orange']
for item in items:
document.add_paragraph(item, style='List Bullet')
# 添加有序列表
document.add_paragraph('刘备',style='List Number')
document.add_paragraph('关羽',style='List Number')
document.add_paragraph('张飞',style='List Number')
# 通过循环来添加多个有序列表项
items = ['First step', 'Second step', 'Third step']
for index, item in enumerate(items, start=1):
document.add_paragraph(f'{index}. {item}', style='List Number')
document.save('../pycharm测试文件/test69附件-列表.docx')
语法:docx.Document()
document = docx.Document()
records = (
('亚瑟','战士英雄'),
('白起','坦克英雄'),
('赵云','刺客英雄'),
('女娲','法师英雄')
)
语法:table = document.add_table(rows = n,cols=n)
table = document.add_table(rows = 1,cols=2)
table.style = 'Dark List' # 背景:Light List-白色,Dark List-黑色
hdr_cells = table.rows[0].cells
hdr_cells[0].text = '姓名'
hdr_cells[1].text = '类型'
可以通过遍历表格的单元格来添加内容
for row in table.rows:
for cell in row.cells:
cell.text = 'Cell content'
for name,type in records:
row_cells = table.add_row().cells
row_cells[0].text = name
row_cells[1].text = type
语法:document.save()
document.save('new_document.docx')
from docx import Document
# 创建word文档对象
document = Document()
# 准备要插入的数据
records = (
('亚瑟','战士英雄'),
('白起','坦克英雄'),
('赵云','刺客英雄'),
('女娲','法师英雄')
)
# 添加表格
table = document.add_table(rows = 1,cols=2)
table.style = 'Dark List' # 背景:Light List-白色,Dark List-黑色
hdr_cells = table.rows[0].cells
hdr_cells[0].text = '姓名'
hdr_cells[1].text = '类型'
# 为表格添加行
for name,type in records:
row_cells = table.add_row().cells
row_cells[0].text = name
row_cells[1].text = type
# 保存到文档中
document.save('../pycharm测试文件/test70附件-插入表格.docx')
python-docx提供了一些内置的表格样式,可以通过属性table.style来设置.
在下面的代码中,我们将表格的样式设置为 “Table Grid”,这是一种带有边框和网格线的样式,还可以根据需要选择其他的表格样式,比如 “Light Shading”、“Light Shading Accent 1” 等。
table.style = 'Table Grid'
也可以自定义表格的边框、字体颜色等样式。
for row in table.rows:
for cell in row.cells:
# 设置边框
cell._element.tcPr.append(docx.oxml.shared.OxmlElement('w:tcBorders'))
cell._element.tcPr.tcBorders.left = docx.oxml.shared.OxmlElement('w:single')
cell._element.tcPr.tcBorders.right = docx.oxml.shared.OxmlElement('w:single')
cell._element.tcPr.tcBorders.top = docx.oxml.shared.OxmlElement('w:single')
cell._element.tcPr.tcBorders.bottom = docx.oxml.shared.OxmlElement('w:single')
# 设置字体颜色
run = cell.paragraphs[0].runs[0]
run.font.color.rgb = docx.shared.RGBColor(0, 0, 255)
table.cell(0, 0).merge(table.cell(0, 1))
合并后的单元格内容会保留在左上角的单元格中,可以通过该单元格的txt属性来设置内容
table.cell(0, 0).text = 'Merged cell'
document = Document("XXX")
document = Document("XXX")
print(document)
语法:读取段落: for paragraph(简写为p) in doc.paragraphs
print(p.text)
读取列表:for index,p in enumerate(document.paragraphs): print(index,p.text)
for index,p in enumerate(document.paragraphs):
print(index,p.text)
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
print(cell.text)
通过使用python-docx库,我们可以轻松地在Python中操作 Word 文档,插入列表和表格等内容。这为我们的自动化办公提供了很大的便利,可以大大提高工作效率。希望本文对你有所帮助!
在实际应用中,你可以根据自己的需求进一步扩展和定制代码,实现更复杂的功能。例如,可以从外部数据源读取数据,动态地插入到表格中;或者根据特定的条件设置表格的样式等。
同时,python-docx库还有很多其他的功能,如设置段落格式、添加页眉页脚等。大家可以参考官方文档来了解更多的用法。
这是我今天学Python的自我想法和对其的理解,有不对的地方请同志们多多包涵,谢谢观看!