当前位置:   article > 正文

python转换xls为xlsx文件_python 将xls修改为xlsx

python 将xls修改为xlsx

#转换xls为xlsx文件


"""
coding:utf-8
@Software:PyCharm
"""
import pandas as pd
from pandas import DataFrame
import openpyxl
from openpyxl import workbook
import xlwt
import xlrd
import numpy as np

def xls_to_xlsx(filename):
    book = xlrd.open_workbook(filename,encoding_override='gbk')   #'gb2312' 或 'gbk' 或 'gb18030' 文件的encoding类型
    sheet_names = book.sheet_names()
    wb = openpyxl.Workbook()
    for sheet_name in sheet_names:
        sh = book.sheet_by_name(sheet_name)
        sheet = wb.active
        sheet.title = sheet_name
        for row_index in range(sh.nrows):
            row = sh.row_values(row_index)
            sheet.append(row)
    return wb
filename = "D:\demo.xls"
xl = xls_to_xlsx(filename)
xl.save("D:\demotest.xlsx")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/888829
推荐阅读
相关标签
  

闽ICP备14008679号