zoukankan      html  css  js  c++  java
  • python-docx

    官方文档

    参考博客

    可以自动生成docx的一个包,

    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')

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

    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")

    生成文件效果图如下:

  • 相关阅读:
    svn和git的优缺点
    idea 的MAVEN Lifecycle 基本用法
    递归SQL---树形结构
    基本:linux命令
    2017年9月22日01:42:08
    简述数据库的设计过程
    HelloH5+搭建
    【Java报错】Message: 3 字节的 UTF-8 序列的字节 2 无效
    css class嵌套
    【java报错】Could not instantiate listener
  • 原文地址:https://www.cnblogs.com/52-qq/p/9753074.html
Copyright © 2011-2022 走看看