使用python和perl语言实现xlsx转化为csv

下面提供使用 Python 和 Perl 两种常用语言的示例代码,来实现将 Excel 文件 (.xlsx) 转换为 CSV 文件。

首先是 Python 的示例代码:

使用 Python 实现 xlsx 转换为 csv:

python 复制代码
# 导入所需模块
import pandas as pd

# 读取 Excel 文件并写入 CSV 文件
def convert_xlsx_to_csv(input_file, output_file):
    df = pd.read_excel(input_file)  # 读取 Excel 文件
    df.to_csv(output_file, index=False)  # 写入 CSV 文件

# 指定输入和输出文件名
input_excel_file = "input.xlsx"
output_csv_file = "output.csv"

# 执行转换
convert_xlsx_to_csv(input_excel_file, output_csv_file)

以上是使用 Python 完成 xlsx 转换为 csv 的示例,接下来是使用 Perl 的示例代码:

使用 Perl 实现 xlsx 转换为 csv:

perl 复制代码
# 导入所需模块
use strict;
use warnings;
use Spreadsheet::ParseXLSX;
use Text::CSV;

# 读取 Excel 文件并写入 CSV 文件
sub convert_xlsx_to_csv {
    my ($input_file, $output_file) = @_;

    my $parser = Spreadsheet::ParseXLSX->new();
    my $workbook = $parser->parse($input_file);

    die "Could not parse $input_file: $!" unless $workbook;

    my $csv = Text::CSV->new({binary => 1});
    open my $fh, ">:encoding(utf8)", $output_file or die "$output_file: $!";

    for my $worksheet ( $workbook->worksheets() ) {
        for my $row (0..$worksheet->maxrow) {
            $csv->combine($worksheet->row($row));
            print $fh $csv->string(), "\n";
        }
    }

    close $fh;
}

# 指定输入和输出文件名
my $input_excel_file = "input.xlsx";
my $output_csv_file = "output.csv";

# 执行转换
convert_xlsx_to_csv($input_excel_file, $output_csv_file);

您可以根据实际情况调整文件名和路径。这两个示例展示了分别使用 Python 和 Perl 实现 xlsx 到 csv 转换的方法。

相关推荐
Java中文社群5 分钟前
用代码书写浪漫,网友直呼“太美了”!
python
@LetsTGBot搜索引擎机器人16 分钟前
打造属于你的 Telegram 中文版:汉化方案 + @letstgbot 搜索引擎整合教程
开发语言·python·搜索引擎·机器人·.net
 梦晓天明2 小时前
12.集合介绍以及数组的使用选择
linux·开发语言·python
嵌入式-老费2 小时前
Easyx图形库应用(python+opencv的图形库开发)
开发语言·python·opencv
m0_64880493_江哥3 小时前
Python实现随机选播视频的示例代码
前端·python·音视频
sunshine~~~3 小时前
【笔记】macOs arm架构安装虚拟机Ubuntu环境:ROS2 + Python开发
arm开发·笔记·python·macos·ros2
云和数据.ChenGuang4 小时前
tensorflow的广播机制
人工智能·python·tensorflow
咕白m6254 小时前
使用 Python 轻松实现 Excel 转 PDF 全指南
python
CodeCraft Studio4 小时前
MPP文件处理组件Aspose.Tasks教程:使用Python在Excel中打开MPP文件
python·ui·excel·csv·mpp·aspose·ms project