python基础 - 处理excel实例

1、提取word内的所有表格内容,写到excel内

安装上python-docx

# coding=utf-8
from docx import Document
from xlwt import Workbook

# 初始化路径
xl_path = f"./path/excel.xls"
word_path = f"./path/word.doc"

# 读word
doc = Document(word_path)
tables = []
for table in doc.tables:
    table_temp = []
    for row in table.rows:
        row_temp = []
        for cell in row.cells:
            row_temp.append(cell.text)
        table_temp.append(row_temp)
    tables.append(table_temp)
tables_2 = list(filter(None, tables))
# 写到表里
Sheet_index = 0
workbook = Workbook(encoding='utf-8')
for table in tables_2:
    worksheet = workbook.add_sheet('sheet' + str(Sheet_index), cell_overwrite_ok=True)
    Sheet_index = Sheet_index + 1
    for rows in table:
        r = table.index(rows)
        for cell in rows:
            c = rows.index(cell)
            worksheet.write(r, c, cell)
workbook.save(xl_path)

2、多个字段名一样的excel合并成一个

import pandas as pd

xl_path = r'D:\path\excel.xls'
res = pd.DataFrame()
for parent, dirnames, filenames in os.walk(xl_path):
    for filename in filenames:
        df = pd.read_excel(os.path.join(parent, filename))
        df_empty = res.append(df, ignore_index=True)
res.to_excel('D:\path\\new_excel.xls', encoding='utf_8_sig')

3、数字转换成excel列序号

def convert_to_title(n):
    result = ""
    while n != 0:
        result = chr((n-1) % 26+65) + result
        n = (n-1)//26
    return result

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值