如何从* .xlsm中提取工作表并将其保存为Python中的* .csv?

时间:2014-05-09 00:21:43

标签: python excel csv spreadsheet xlsm

我有一个* .xlsm文件,里面有20张。 我希望单独保存几张* .csv(格式化损失很好)。 已经尝试过xlrd-xlwt和win32com库但无法通过。 任何人都可以提供一个代码片段,它在Python中进行上述处理吗?我有其他python依赖项,所以没有其他语言可以工作。 感谢

4 个答案:

答案 0 :(得分:4)

xlrd也可以在xlsm文件上正常工作。我用随机的xlsm文件测试了代码,它运行得很好。

import csv
import xlrd

workbook = xlrd.open_workbook('test.xlsx')
for sheet in workbook.sheets():
    with open('{}.csv'.format(sheet.name), 'wb') as f:
        writer = csv.writer(f)
        writer.writerows(sheet.row_values(row) for row in range(sheet.nrows))

如果您遇到编码问题,请尝试以下代码:

import csv
import xlrd

workbook = xlrd.open_workbook('test.xlsm')
for sheet in workbook.sheets():
    if sheet.name == "Sheet_name_from_xlsm_file":
        with open('{}.csv'.format(sheet.name), 'wb') as f:
            writer = csv.writer(f)
            for row in range(sheet.nrows):
                out = []
                for cell in sheet.row_values(row):
                    try:
                        out.append(cell.encode('utf8'))
                    except:
                        out.append(cell)
                writer.writerow(out)

答案 1 :(得分:2)

import pandas as pd

import xlrd

import openpyxl #required for xlrd 2.0.1 and higher

df = pd.read_excel('your_excel_file_name.xlsm', sheet_name='your_sheet_name')
df.to_csv('your_new_name.csv')

答案 2 :(得分:1)

您可以使用熊猫轻松做到这一点

    通过以下方式
  1. 安装熊猫 xlrd 依赖项

    • pip3安装熊猫
    • pip3 install xlrd(熊猫要求)
  2. 现在只需使用 read_excel 读取xlsm文件。这是一个演示:-

import pandas as pd

# YOU MUST PUT sheet_name=None TO READ ALL CSV FILES IN YOUR XLSM FILE
df = pd.read_excel('YourFile.xlsm', sheet_name=None)

# prints all sheets
print(df)

# prints all sheets name in an ordered dictionary
print(df.keys())

# prints first sheet name or any sheet if you know it's index
first_sheet_name = list(df.keys())[0]
print(first_sheet_name)

# prints first sheet or any sheet if know it's name
print(df[first_sheet_name])

# export first sheet to file
df[first_sheet_name].to_csv('FirstSheet.csv')

# export all sheets 
for sheet_name in list(df.keys()):
   df[sheet_name].to_csv(sheet_name + 'Sheet.csv')


# USE IT IN MULTIPLE WAYS #

答案 3 :(得分:1)

import pandas as pd

# YOU MUST PUT sheet_name=None 
df = pd.read_excel('YourFile.xlsm', sheet_name=None)

# prints all sheets
print(df)