python写文档(word\Excel)

#创建并写入word文档
import docx

#创建内存中的word文档对象
file=docx.Document()

#写入若干段落
file.add_paragraph("啊")
file.add_paragraph("山,真的很高")
file.add_paragraph("高的吓人")
file.add_paragraph("啊!!")
file.add_paragraph("实在是高")

#保存
file.save("D:\\temp\\writeResult.docx")

###word单元格

    doc = docx.Document()
    rowNums = 0
    colNums = 1
    for s in summary:
        if s=='\n':
            rowNums +=1
    table = doc.add_table(rowNums, colNums)
    temRow = 0
    temStr = "";
    for s in  summary:

        if s == '\n':
            cell =  table.cell(temRow,0)
            cell.paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 水平居中
            # cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER  # 垂直居中
            cell.text= temStr
            temStr = ""
            temRow +=1
        else: temStr += s
    doc.save(target_path + "2022-8-30.docx")

在这里插入图片描述
python往word文档中写入表格、段落、标题、图片…(超级全)

    doc = docx.Document()
    # 设置全局属性
    # doc.styles['Normal'].font.size = Pt(8)
    # doc.styles['Normal'].font.name = u'Times New Roman'
    paragraph= doc.add_paragraph(summary)
    paragraph.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.JUSTIFY
    doc.save(target_path + "2022-8-30.docx")

在这里插入图片描述

excel

    book = xlwt.Workbook() #创建Excel
    sheet = book.add_sheet('sheet1')  # 创建sheet页
    row = 0
    sheet.write(0, row, "一元回归实验报告")
    row = 1  # 从表格的第二行开始写入数据
    res =""
    for i in str(summary):
        res+= i;
        if(i == '\n'):
            row  +=1
            sheet.write(row,0, res)
            res  =""
    book.save(target_path + "\\result.xlsx")

在这里插入图片描述

    with open(target_path + "\\result.xlsx",
              "w", encoding="utf-8") as f:
        f.write(str(summary))

在这里插入图片描述
xlrd

    # 1 打开工作薄(workbook.xlsx)
    excel_ = xlrd.open_workbook(target_path + "\\aa.xlsx")
    # 2 定位要读取内容的工作表
    # 两种方法:(1)索引;(2)表的名字
    Table = excel_.sheet_by_index(0)  # 通过索引定位工作表,索引从0开始
    # Table_1 = excel_.sheet_by_name('Sheet1')  # 通过表的名字定位工作表

插入图片


    workbook = xlsxwriter.Workbook(target_path + "\\aa.xlsx")
    worksheet = workbook.add_worksheet("aa")
    worksheet.insert_image('A21', target_path+"\\result-0-1.png",{
    
    'x_offset': 15, 'y_offset': 10})
    workbook.close()

合并单元格

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet("aa")
worksheet.write_merge(0, 40, 0, 9, str(summary))
workbook.save(target_path+"\\aa.xlsx")

水平、垂直居中

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet("aa")
style = xlwt.XFStyle()  # 创建一个样式对象,初始化样式
al = xlwt.Alignment()
al.horz = 0x02  # 设置水平居中
al.vert = 0x01  # 设置垂直居中
style.alignment = al
worksheet.write_merge(0, 40, 0, 9, str(summary),style)
workbook.save(target_path+"\\aa.xlsx")

在这里插入图片描述

    book = xlsxwriter.Workbook(target_path + "\\result.xlsx")
    sheet = book.add_worksheet("result")
    merge_format = book.add_format({
    
    
        'bold': True,
        'border': 6,
        # 'align': 'center',  # 水平居中
        'valign': 'vcenter',  # 垂直居中
        'fg_color': '#D7E4BC',  # 颜色填充
    })
    sheet.merge_range(0,0,30,10,str(summary),merge_format)
    sheet.insert_image('A41', target_path + "\\result-0-1.png", {
    
    'x_offset': 15, 'y_offset': 10})
    book.close()

在这里插入图片描述
在这里插入图片描述

PYTHON XLSXWRITER MODULE 使用之单元格合并 WORKSHEET.MERGE_RANGE()

猜你喜欢

转载自blog.csdn.net/weixin_42382758/article/details/126550839