当前位置: 首页 > news >正文

上海建设安全生产协会网站京津冀协同发展对河北的影响

上海建设安全生产协会网站,京津冀协同发展对河北的影响,无锡市建设招标网站,网站设计的工作要求使用docx库#xff0c;可以执行各种任务 创建新文档#xff1a;可以使用库从头开始或基于模板生成新的Word文档。这对于自动生成报告、信函和其他类型的文档非常有用。修改现有文档#xff1a;可以打开现有的Word文档#xff0c;并使用库修改其内容、格式、样式等。这对于…使用docx库可以执行各种任务 创建新文档可以使用库从头开始或基于模板生成新的Word文档。这对于自动生成报告、信函和其他类型的文档非常有用。修改现有文档可以打开现有的Word文档并使用库修改其内容、格式、样式等。这对于自动更新遵循特定结构的文档特别方便。添加内容可以使用库向文档添加段落、标题、表格、图像和其他元素。这有助于用数据动态填充文档。格式化该库允许将各种格式化选项应用于文档中的文本和元素例如更改字体、颜色、对齐方式等。提取信息还可以从现有Word文档中提取文本、图像、表格和其他内容以便进一步分析 Docx functions 1. 文档创建和保存 Document(): 创建一个新的word文档Document.save(‘filename.docx’)保存一个document 称为文件*.docx 2. Paragraphs and Text (段落和文本) add_paragraph(‘text’) 添加具有指定文本text的新段落Paragraphs。paragraph.text获取或设置段落的文本内容。 3. Headings 标题可以设置几级标题 add_heading(‘text’, leveln): 添加具有指定文本和级别的标题 (1 to 9). 4. Styles and Formatting 样式与格式 paragraph.style ‘StyleName’: 应用特定的段落样式run paragraph.add_run(‘text’): 添加一段具有特定格式的文本run.bold, run.italic, etc.: 对管路run应用格式设置 5. Tables (表格操作) add_table(rows, cols): 添加具有指定行数和列数的表table.cell(row, col): 获取表中的特定单元格cellcell.text:获取或设置单元格的文本内容table.rows, table.columns:访问表的行和列 6. Images(图片操作) document.add_picture(‘image_path’): 向文档中添加图像run.add_picture(‘image_path’): 将图像添加到特定管道run中, 比如简历照片位置固定的 7. Document Properties 文档属性 document.core_properties.title: 设置文档的标题document.core_properties.author: 设置文档的作者document.core_properties.keywords: 设置文档的关键词 8. Sections and Page Setup 分区和页面设置 section document.sections[0]: 获取文档的第一部分 Get the first section of the documentsection.page_width, section.page_height: 设置页面尺寸Set page dimensions 9. Lists 列表 就是markdown中的list,比如下面的这两个就是无序的大标题123…就是有序的 add_paragraph(‘text’, style’ListBullet’):创建无序列表 Create a bulleted listadd_paragraph(‘text’, style’ListNumber’): 创建有序列表Create a numbered list. 10. Hyperlinks (超链接) run.add_hyperlink(‘url’, ‘text’): 给当前管道run内的特定文本text添加超链接(Add a hyperlink to a run) 11. Document Modification 文件修改 document.paragraphs: 访问文档中的所有段落Access all paragraphs in the documentdocument.tables: 访问文档中的所有表格Access all tables in the documentdocument.styles: 访问和操作文档样式Access and manipulate document styles 12. Document Reading文档读取 Document(‘filename.docx’): 读取一个存在的word文件document.paragraphs[0].text: 访问第一段paragraphs的文本text 小例子 1. Installation (安装) pip install python-docx2. 创建一个新的word文档 创建一个包含文本、标题、表格、图像和格式的文档 Create a new document.创建一个新的document 对象Add a title with centered alignment.添加一个标题title并居中对齐Add a paragraph with bold and italic text.添加带有粗体和斜体文本的段落Add a heading and a bulleted list.添加标题(heading)和项目符号列表Add a table with custom column widths.添加table,并自定义列宽Add an image to the document.添加图片Save the document with the name ‘example_document.docx’.保存文件文件名为 example_document.docx from docx import Document from docx.shared import Pt from docx.enum.text import WD_ALIGN_PARAGRAPH# Create a new document doc Document()# Add a title title doc.add_heading(Document Creation Example, level1) title.alignment WD_ALIGN_PARAGRAPH.CENTER# Add a paragraph with bold and italic text paragraph doc.add_paragraph(This is a sample document created using the python-docx library.) run paragraph.runs[0] run.bold True run.italic True# Add a heading doc.add_heading(Section 1: Introduction, level2)# Add a bulleted list list_paragraph doc.add_paragraph() list_paragraph.add_run(Bullet 1).bold True list_paragraph.add_run( - This is the first bullet point.) list_paragraph.add_run(\n) list_paragraph.add_run(Bullet 2).bold True list_paragraph.add_run( - This is the second bullet point.)# Add a table doc.add_heading(Section 2: Data, level2) table_1 doc.add_table(rows1, cols2) table_1.style Table Grid table_1.autofit False table_1.allow_autofit False for row in table_1.rows:for cell in row.cells:cell.width Pt(150) table_1.cell(0, 0).text cat table_1.cell(0, 1).text dogtable_2 doc.add_table(rows3, cols3) table_2.style Table Grid table_2.autofit False table_2.allow_autofit False for row in table_2.rows:for cell in row.cells:cell.width Pt(100) table_2.cell(0, 0).text Name table_2.cell(0, 1).text Age table_2.cell(0, 2).text City for i, data in enumerate([(Alice, 25, New York), (Bob, 30, San Francisco), (Charlie, 22, Los Angeles)], start0):print(i, data)table_2.cell(i, 0).text data[0]table_2.cell(i, 1).text data[1]table_2.cell(i, 2).text data[2]# Add an image doc.add_heading(Section 3: Image, level2) doc.add_paragraph(Here is an image of cat:) doc.add_picture(../imgs/cat.jpg, widthPt(300))# Save the document doc.save(../word_files/example_new_document.docx)结果哈哈样式有点丑暂时忽略… 3. 修改现有的word文档 open an existing Word document (‘existing_document.docx’).( 读取一个存在的word文档)Modify the text, formatting, and alignment of the first paragraph.修改第一段的文本、格式和对齐方式Add a new heading.添加一个新的标题Add a new paragraph with a hyperlink.添加带有超链接的新段落Add a new table with custom column widths and data.添加一个具有自定义列宽和数据的新表Save the modified document as ‘modified_document.docx’.将修改后的文档另存为“modified_document.docx” import docx from docx import Document from docx.shared import Pt from docx.enum.text import WD_ALIGN_PARAGRAPHdef add_hyperlink(paragraph, url, text, color, underline):A function that places a hyperlink within a paragraph object.:param paragraph: The paragraph we are adding the hyperlink to.:param url: A string containing the required url:param text: The text displayed for the url:return: The hyperlink object# This gets access to the document.xml.rels file and gets a new relation id valuepart paragraph.partr_id part.relate_to(url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_externalTrue)# Create the w:hyperlink tag and add needed valueshyperlink docx.oxml.shared.OxmlElement(w:hyperlink)hyperlink.set(docx.oxml.shared.qn(r:id), r_id, )# Create a w:r elementnew_run docx.oxml.shared.OxmlElement(w:r)# Create a new w:rPr elementrPr docx.oxml.shared.OxmlElement(w:rPr)# Add color if it is givenif not color is None:c docx.oxml.shared.OxmlElement(w:color)c.set(docx.oxml.shared.qn(w:val), color)rPr.append(c)# Remove underlining if it is requestedif not underline:u docx.oxml.shared.OxmlElement(w:u)u.set(docx.oxml.shared.qn(w:val), none)rPr.append(u)# Join all the xml elements together add add the required text to the w:r elementnew_run.append(rPr)new_run.text texthyperlink.append(new_run)paragraph._p.append(hyperlink)return hyperlink # Open an existing documentdoc Document(../word_files/example_new_document.docx)# Access the first paragraph and modify its text and formatting first_paragraph doc.paragraphs[0] first_paragraph.text Updated Text: 宫廷玉液酒一百八一杯。 run first_paragraph.runs[0] run.bold True #加粗 run.italic True #斜体 run.font.size Pt(20) #字号 first_paragraph.alignment WD_ALIGN_PARAGRAPH.CENTER #居中对齐# Add a new heading doc.add_heading(New Section, level1)# Add a new paragraph with a hyperlink new_paragraph doc.add_paragraph(Visit my bolg website: ) hyperlink add_hyperlink(new_paragraph,https://blog.csdn.net/weixin_40959890/article/details/137598605?spm1001.2014.3001.5501,Python docx在Python中创建和操作Word文档,FF8822, True) # run new_paragraph.add_run(Python docx在Python中创建和操作Word文档) # run.hyperlink.address https://blog.csdn.net/weixin_40959890/article/details/137598605?spm1001.2014.3001.5501# Add a new table doc.add_heading(Table Section, level2) table doc.add_table(rows4, cols4) table.style Table Grid table.autofit False table.allow_autofit False for row in table.rows:for cell in row.cells:cell.width Pt(100) table.cell(0, 0).text Name table.cell(0, 1).text Age table.cell(0, 2).text City for i, data in enumerate([(David, 128, London), (Emma, 135, New York), (John, 122, Los Angeles)], start1):table.cell(i, 0).text data[0]table.cell(i, 1).text data[1]table.cell(i, 2).text data[2]# Save the modified document doc.save(../word_files/example_modified_document.docx)结果看一下依旧很丑哈哈但是修改成功了 参考 word插入超链接 examples python-docx文档 pypi python-docx
http://www.zqtcl.cn/news/324615/

相关文章:

  • 泰安北京网站建设公司个人自我介绍网页
  • 网站建设适应全屏如何自动深圳市哪里最繁华
  • 杭州网站推广公司阿里云wordpress 安装目录
  • 厦门优秀网站建设app项目开发流程
  • 工作设计室网站海外网站代理
  • 室内设计官方网站没网站怎么做cpa
  • 哪个网站做欧洲旅游攻略好wordpress编辑器字体大小
  • aspcms 手机网站wordpress 刷浏览量
  • dw网站首页的导航怎么做网站建设企业建站模板
  • 平台型网站建设网站关键词优化seo
  • 齿轮机械东莞网站建设技术支持热搜词排行榜关键词
  • 河南专业做网站网站推广优化c重庆
  • 温州网站建设钱建设工程公司网站
  • 做笑话网站全国大学生职业生涯规划大赛官网
  • 便宜购 网站建设平台推广引流怎么做
  • 怎么用记事本做钓鱼网站制作公司网页的步骤
  • 机械设备东莞网站建设智慧软文网站
  • 个人网站需不需要搭建服务器蘑菇短视频2023版特色功能
  • 网站建设公司是什么东兰县建设局网站
  • 网站优化排名方案软件发布网
  • 企业网站开发价钱低企业策划案例
  • 网站建设帐号网站导入页欣赏
  • ftp 迁移 网站建筑公司商标logo设计
  • 没钱怎么做网站wordpress 链接修改插件
  • 建一个网站需要多久建设银行官网登录入口
  • 贸易公司网站制作邢台哪里做网站
  • 2018网站开发的革新帮别人起名 做ppt的网站
  • 有哪些做问卷调查赚钱的网站6长沙网站建设技术
  • 烟台做网站需要多少钱制作ppt的软件是什么
  • 泉州模板开发建站wordpress显示一个类目