python将excel xlsx转换(读取并保存)为xlsx [英] Python convert (read & save) excel xlsx to xls

查看:639
本文介绍了python将excel xlsx转换(读取并保存)为xlsx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在保留Excel文件格式的同时将现有的xlsx Excel文件转换为xls?我使用Anaconda Python 3,所以我不确定我可以使用xlutils ...由于存在许多不兼容性,因此无法通过conda install xlutils安装它.所以现在我使用的代码不带xlutils.copy():

import xlrd, xlwt

wb = xlrd.open_workbook(my_xlsx_excel_file)
# wb = xlutils.copy(wb)
wb.save(my_xlsx_excel_file[:-1])

我收到此错误:

AttributeError: 'Book' object has no attribute 'save'

谢谢!

解决方案

第一件事:为什么要转换为.xls?这通常表明您在过程中的某个位置使用了过时的工具,使用新工具比将数据转换为旧格式可能更好.

但是,如果在保留格式的同时确实需要转换为.xls,则此时唯一可行的选择是使用Excel本身.您没有说要使用哪个平台,但是如果是Windows或Mac,并且已经安装了Excel,那么自动化Excel的最直接方法就是 xlwings . 原则上,这将允许您使用Python在Excel(Microsoft Excel的实际运行实例)中打开.xlsx文件,并另存为" .xls文件.

我说原则上"是因为我个人不知道如何在xlwings中做到这一点. (我并没有真正使用该软件包.)在幕后,xlwings依赖于 pywin32 在Windows上为 appscript 在Mac上,因此您可以直接使用这些较低级别的软件包.

例如,如果您使用的是Windows,则可以执行以下操作:

from win32com.client import Dispatch

xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Add(my_xlsx_excel_file)
wb.SaveAs(my_xlsx_excel_file[:-1], FileFormat=56)
xl.Quit()

56是一个魔法常数表示Excel 97-2003格式(对于Windows).

自然,在具有appscript的Mac上应该有相应的方法来执行此操作.请注意,文件格式常量 可能可能不同比在Windows上要好.

How can I convert an existing xlsx Excel file into xls while retaining my Excel file formatting? I use Anaconda Python 3, so I'm not sure I can use xlutils... I was not able to install it via conda install xlutils because of lots of incompatibilities. So now I use this code without the xlutils.copy():

import xlrd, xlwt

wb = xlrd.open_workbook(my_xlsx_excel_file)
# wb = xlutils.copy(wb)
wb.save(my_xlsx_excel_file[:-1])

And I get this error:

AttributeError: 'Book' object has no attribute 'save'

Thank you!

解决方案

First things first: Why do you want to convert to .xls? This is usually a sign that you are using outdated tools somewhere in the process, and it might be better to use newer tools rather than convert the data to an older format.

But, if you really need to convert to .xls while preserving formatting, your only realistic choice at this time is to use Excel itself. You didn't say which platform you are using, but if it's Windows or Mac, and you have Excel installed, then the most straightforward way to automate Excel is probably xlwings. In principle this will allow you to use Python to open the .xlsx file in Excel (an actual, running instance of Microsoft Excel) and do "save as" to a .xls file.

I say "in principle" because I don't personally know how to do it in xlwings. (I don't really use that package.) Under the covers, xlwings is relying on pywin32 on Windows and appscript on Mac, so you could use those lower-level packages directly.

For example, if you are on Windows, you could do this:

from win32com.client import Dispatch

xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Add(my_xlsx_excel_file)
wb.SaveAs(my_xlsx_excel_file[:-1], FileFormat=56)
xl.Quit()

The 56 is a magic constant indicating Excel 97-2003 format (for Windows).

Naturally, there should be a corresponding way to do this on a Mac with appscript. Just be aware that the file format constants may be different than on Windows.

这篇关于python将excel xlsx转换(读取并保存)为xlsx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆