前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python docx

python docx

作者头像
用户5760343
发布2022-05-14 10:19:15
5020
发布2022-05-14 10:19:15
举报
文章被收录于专栏:sktjsktj

https://blog.csdn.net/sinat_37005367/article/details/77855359

复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">from docx import Document from docx.shared import Inches document = Document() for row in range(9): t = document.add_table(rows=1, cols=1, style='Table Grid') t.autofit = False # 很重要! w = float(row) / 2.0 t.columns[0].width = Inches(w) document.save('table-step.docx')</pre>

(javascript:void(0); "复制代码")

会在当前目录下生成一个.docx文件,然后里面会自动生成表格。。

(javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">from docx import Document document = Document() paragraph = document.add_paragraph('Lorem ipsum dolor sit amet') prior_paragraph = paragraph.insert_paragraph_before('Lorem ipsum before') document.add_heading(text='The REAL meaning of the universe', level=0) document.add_heading('The REAL meaning of the universe') document.add_heading(text='The role of dolphins', level=2) document.add_page_break() table = document.add_table(rows=2, cols=2) cell = table.cell(0, 1) cell.text = 'parrot, possibly!' row = table.rows[1] row.cells[0].text = 'Foo bar to you!' row.cells[1].text = 'And a hearty foo bar to you too sir!' for row in table.rows: for cell in row.cells: print('====>', cell.text)

row_count = len(table.rows) col_count = len(table.columns) print('row_count:', row_count, 'col_count:', col_count) rows = table.add_row() document.add_page_break() document.add_heading(text='The REAL meaning of the universe', level=0) document.add_heading('The REAL meaning of the universe') document.add_heading(text='The role of dolphins', level=2) document.save("xxx.docx")</pre>

(javascript:void(0); "复制代码")

生成文件效果图如下:

image

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档