简体   繁体   English

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

[英]How to extract sheet from *.xlsm and save it as *.csv in Python?

I have a *.xlsm file which has 20 sheets in it.我有一个 *.xlsm 文件,里面有 20 张纸。 I want to save few sheets as *.csv (formatting loss is fine) individually.我想单独将几张纸保存为 *.csv(格式丢失很好)。 Already tried xlrd-xlwt and win32com libraries but could not get through.已经尝试过 xlrd-xlwt 和 win32com 库,但无法通过。 Can anybody please provide a code snippet which does the above processing in Python?任何人都可以提供在 Python 中进行上述处理的代码片段吗? I have other python dependencies so no other language would work.我有其他 python 依赖项,所以没有其他语言可以工作。 Thanks谢谢

xlrd should work fine on xlsm files as well. xlrd 也应该在 xlsm 文件上正常工作。 I tested the code with a random xlsm file, and it worked perfectly.我用一个随机的 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))

If you've encoding issues, try the code below:如果您遇到编码问题,请尝试以下代码:

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)

You can do this easily with pandas你可以用熊猫轻松做到这一点

  1. Install pandas and xlrd dependencies by following按照以下步骤安装pandasxlrd依赖项

    • pip3 install pandas pip3 安装熊猫
    • pip3 install xlrd (required by pandas) pip3 install xlrd (pandas 需要)
  2. Now simply read xlsm file using read_excel .现在只需使用read_excel读取 xlsm 文件。 Here is a demo:-这是一个演示:-

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 #
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')

I have a *.xlsm file which has 20 sheets in it.我有一个* .xlsm文件,其中有20张纸。 I want to save few sheets as *.csv (formatting loss is fine) individually.我想将几张纸单独保存为* .csv(格式丢失是可以的)。 Already tried xlrd-xlwt and win32com libraries but could not get through.已经尝试过xlrd-xlwt和win32com库,但无法通过。 Can anybody please provide a code snippet which does the above processing in Python?谁能提供一个使用Python进行上述处理的代码段? I have other python dependencies so no other language would work.我还有其他python依赖项,因此没有其他语言可以使用。 Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:从stdout中提取模式并保存在csv中 - Python: extract pattern from stdout and save in csv 如何使用 Python 从大型 .csv 文件中迭代地提取和保存 .csv 数据块? - How to extract and save in .csv chunks of data from a large .csv file iteratively using Python? 如何下载 xlsm 文件并阅读 python 中的每张纸? - How do I download an xlsm file and read every sheet in python? Python-多个XLSX / XLSM到CSV - Python - Multiple XLSX/XLSM to CSV 如何从 json 文件中提取特定数据并将其保存为 python 文件中的 csv 文件 - How to extract specific data from json file and save it as csv file in python 通过Python DPKT从PCAP提取所有协议数据并另存为CSV - Extract all protocols data from PCAP by Python DPKT and Save as CSV 如何从python中的csv文件中提取字符串 - How to extract the strings from csv file in python PYTHON:从 csv 表读取 - PYTHON: Read from csv sheet 如何使用python从Excel工作表中提取超链接单元格 - How to extract the hyperlink cell from excel sheet using python Python BeautifulSoup and Pandas extract table from list of urls and save all the tables into single dataframe or save as csv - Python BeautifulSoup and Pandas extract table from list of urls and save all the tables into single dataframe or save as csv
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM